✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

  1. Collection Framework

List의 removeAll() 과 clear() 비교

PreviousList, Set, Map InterfaceNextList Interface - ArrayList

Last updated 3 years ago

Was this helpful?

removeAll()과 clear() 시간 복잡도 비교 먼저 removeAll()의 시간복잡도는 반복문이 1개라면 O(n) 선형 알고리즘이고, 반복문이 2개 중첩되있다면 O(n^2)이 된다. 반복문이 몇번 실행되었는지 주목해야한다!⭐️ 예를 들어 길이가 n인 리스트가 있고, 제거할 요소가 m개라면 이 때 removeAll()의 시간복잡도는 O(nm)이다. 하지만 길이가 n인 리스트에서 제거할 요소 또한 n개라면 시간 복잡도는 O(n^2)이 되겠다.

  1. removeAll() : 전체 리스트 중에서 특정 Collection에 포함된 요소들만 제거한다! removeAll(Collection<?> c) > batchRemove(Collection<?> c, boolean complement) > contain() 아래 코드에서 li.removeAll(m)은 li에서 특정 Collection인 m에 포함된 요솓들만 제거하는 것이다!

public class Main{
    public static void main(String[] args){
        List<Integer> li = new ArrayList<>();
        Set<Integer> m = new HashSet<>();
        for(int i=0;i<100000;i++){
            li.add(i);m.add(i);
        }
        li.removeAll(m);
    }
}

2. clear() : 전체 리스트를 비운다! 시간복잡도는 O(n)

컬렉션 별 add, remove, contain, get 메서드 시간 복잡도 비교 =>특히 Set > HashSet의 contain()은 key값으로 바로 찾아버리기 때문에 O(1)로 매우 빠르다!

https://unordinarydays.tistory.com/194