안녕하세요, 여행벌입니다.

저번 포스팅에 이어서 컬렉션 프레임워크의 기반이 되는 Collection<E> 인터페이스의 메소드에 대해서 정리해보겠습니다.


Collection<E>

 모든 컬렉션클래스들은 Collection<E> 인터페이스를 직/간접적으로 구현하고 있습니다.

 따라서, Collection<E> 인터페이스에는 컬렉션을 다루는데 공통적으로 필요한 메소드들을 지원해주고 있습니다. 그럼 Collection 인터페이스에서 제공해주는 메소드들을 하나하나 정리해보도록 하겠습니다.

 예시는 컬렉션 클래스 중 하나인 LinekdList 컬렉션 클래스를 이용해서 보여드리겠습니다. 다음 포스팅에서 다룰 컬렉션 클래스이니 그냥 Collection 인터페이스에서 지원해주는 메소드 활용 방식만 참고하시면 될 것 같습니다.

boolean add​(E e)

 이름 그대로 해당 컬렉션에 요소를 추가해줍니다. 추가가 성공한다면 true 를 반환하고, 메모리 문제 등으로 요소 추가가 실패한다면 false를 반환합니다.

1
2
LinkedList<Integer> list = new LinkedList<>();
list.add(5);
cs

boolean add(int index, E e)

 원하는 index에 요소를 추가할 수 있습니다.

1
2
3
4
5
LinkedList<Integer> list = new LinkedList<>();
list.add(5); // list = 5
list.add(7); // list = 5 - 7
list.add(16); // 1번 인덱스에 추가
// list = 5 6 7 로 변환!
cs

boolean addAll(Collection<? extends E> c)

 컬렉션 c에 있는 모든 요소들을 추가해줍니다.

1
2
3
4
5
LinkedList<Integer> list = new LinkedList<>();
list.add(5); // list = 5
list.add(7); // list = 5 - 7
LinkedList<Integer> list2 = new LinkedList<>();
list2.addAll(list); // list2 = 5 - 7
cs

 list 에 있는 모든 요소들을 list2에 addAll 해주면 list2 도 요소 5와 7이 생깁니다.

void clear()

 clear 메소드는 말 그대로 해당 컬렉션의 모든 요소들을 제거해줍니다.

1
2
3
4
LinkedList<Integer> list = new LinkedList<>();
list.add(5); // list = 5
list.add(7); // list = 5 - 7
list.clear(); // list 는 empty
cs

boolean contains(Object o)

 contains 메소드는 컬렉션이 해당 요소를 가지고 있다면 true, 아니라면 false를 return 해줍니다.

1
2
3
4
5
LinkedList<Integer> list = new LinkedList<>();
list.add(5); // list = 5
list.add(7); // list = 5 - 7
System.out.println(list.contains(5)); // true
System.out.println(list.contains(3)); // false
cs

boolean containsAll(Collecton<?> c)

 해당 컬렉션이 컬렉션 c의 모든 요소를 가지고 있다면 true, 아니라면 false를 return 해줍니다.

1
2
3
4
5
6
7
8
9
LinkedList<Integer> list = new LinkedList<>();
list.add(5); // list = 5
list.add(7); // list = 5 - 7
LinkedList<Integer> list2 = new LinkedList<>();
list2.add(5);
list2.add(7);
list2.add(9);
System.out.println(list2.containsAll(list)); // true
System.out.println(list.containsAll(list2)); // false
cs

int size()

 해당 컬렉션의 size를 return 해줍니다.

1
2
3
4
LinkedList<Integer> list = new LinkedList<>();
list.add(5); // list = 5
list.add(7); // list = 5 - 7
System.out.println(list.size()); // 2
cs

boolean isEmpty()

 해당 컬렉션이 비어있다면 true, 아니라면 false를 return 해줍니다.

1
2
3
4
5
6
LinkedList<Integer> list = new LinkedList<>();
list.add(5); // list = 5
list.add(7); // list = 5 - 7
LinkedList<Integer> list2 = new LinkedList<>();
System.out.println(list.isEmpty()); // false
System.out.println(list2.isEmpty()); // true
cs

boolean remove(Object o)

 컬렉션에서 해당 요소를 찾아서 제거합니다. 제거가 성공하면 true, 실패하면 false 를 반환합니다.

1
2
3
4
5
LinkedList<Integer> list = new LinkedList<>();
list.add(5); // list = 5
list.add(7); // list = 5 - 7
System.out.println(list.remove((Object)5)); // true
System.out.println(list.remove((Object)5)); // false
cs

boolean removeAll(Collection<?> c)

 해당 컬렉션에서 모든 요소를 제거합니다. 제거가 성공하면 true, 실패하면 false 를 반환합니다.

1
2
3
4
LinkedList<Integer> list = new LinkedList<>();
list.add(5); // list = 5
list.add(7); // list = 5 - 7
System.out.println(list.removeAll(list)); // true
cs

Collection 인터페이스에서 자주 사용되는 메소드들을 정리해보았습니다.

모든 컬렉션 클래스들에게 공통적으로 지원해되는 메소드로 꼭 익혀두시길 바랍니다.

 

 

 

+ Recent posts