티스토리 뷰

PL/Java

[Java] Collection: List, Set, Map

SweetDev 2021. 4. 29. 18:56

Java의 Collection에 대해서 알아보자!

이런 애들은 java.util에 있다. 

 

Collection

  • List
    • ArrayList
    • Vector
    • LinkedList
  • Set
    • HashSet
    • TreeSet

따로

  • Map
    • HashMap
    • Hashtable
    • TreeMap
    • Properties

List의 종류로 ArrayList, Vector, LinkedList를 써놓았는데 정확한 의미는, List 인터페이스를 구현한 종류 중에서 ArrayList, Vector, LinkedList가 있는 것이다. List와 Set은 둘다 Collection인데 Map은 별개로 분리되어 있다.  List와 Set은 객체를 추가, 삭제, 검색 하는 방법에 많은 공통점들이 있다. 반면 Map은 키와 값을 하나의 쌍으로 묶어 관리하기 때문에 많이 다르다.

1. List 

List 인터페이스의 메소드들은 다음과 같다. 

 

List는 객체 주소의 배열이다. 동일한 객체를 중복 저장하면 같은 주소가 참조된다. null을 저장하면 객체를 아예 참조하지 않는다. 

 

boolean add(E e)

Appends the specified element to the end of this list (optional operation).

void add(int index, E element)

Inserts the specified element at the specified position in this list (optional operation).

boolean addAll(Collection<? extends E> c)

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).

boolean addAll(int index, Collection<? extends E> c)

Inserts all of the elements in the specified collection into this list at the specified position (optional operation).

void clear()

Removes all of the elements from this list (optional operation).

boolean contains(Object o)

Returns true if this list contains the specified element.

boolean containsAll(Collection<?> c)

Returns true if this list contains all of the elements of the specified collection.

boolean equals(Object o)

Compares the specified object with this list for equality.

E get(int index)

Returns the element at the specified position in this list.

int hashCode()

Returns the hash code value for this list.

int indexOf(Object o)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

boolean isEmpty()

Returns true if this list contains no elements.

Iterator<E> iterator()

Returns an iterator over the elements in this list in proper sequence.

int lastIndexOf(Object o)

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

ListIterator<E> listIterator()

Returns a list iterator over the elements in this list (in proper sequence).

ListIterator<E> listIterator(int index)

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

E remove(int index)

Removes the element at the specified position in this list (optional operation).

boolean remove(Object o)

Removes the first occurrence of the specified element from this list, if it is present (optional operation).

boolean removeAll(Collection<?> c)

Removes from this list all of its elements that are contained in the specified collection (optional operation).

default void replaceAll(UnaryOperator<E> operator)

Replaces each element of this list with the result of applying the operator to that element.

boolean retainAll(Collection<?> c)

Retains only the elements in this list that are contained in the specified collection (optional operation).

E set(int index, E element)

Replaces the element at the specified position in this list with the specified element (optional operation).

int size()

Returns the number of elements in this list.

default void sort(Comparator<? super E> c)

Sorts this list according to the order induced by the specified Comparator.

default Spliterator<E> spliterator()

Creates a Spliterator over the elements in this list.

List<E> subList(int fromIndex, int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

Object[] toArray()

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

<T> T[] toArray(T[] a)

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

사실 내가 위에서 쓴 ArrayList, Vector, LinkedList외에도 많은 클래스들이 List인터페이스를 구현한다.

 

1.1 ArrayList

ArrayList에 특정 인덱스의 객체를 제거하면 뒤에 있던 애들이 하나씩 당겨진다. 마찬가지로 삽입해도 하나씩 밀린다. 따라서 빈번한 객체 삽입과 삭제가 일어나는 곳에서는 ArrayList보다 LinkedList를 쓰는 것이 더 좋다. 그러나 맨 마지막에 객체를 추가하거나 인덱스로 검색하는 경우에는 LinkedList가 더 좋다. 

List<String> list = new ArrayList<String>();

1.2 Vector

Vector은 Thread Safe 한 ArrayList이다. 

List<E> list = new Vector<E>();

1.3 LinkedList

LinkedList는 인접 참조를 링크해서 체인처럼 관리한다!!!

List<E> list = new LinkedList<E>();

2. Set

Set은 저장순서가 유지되지 않는다!! 또한 중복되는 객체는 저장할 수 없다. null도 여러개는 불가능하다. 

2.1 HashSet

hashCode를 얻어내서 비교하고 hashCode가 같으면 다시 equals()로 객체를 비교해서, true이면 저장하지 않는다.

@Override해서 hashCode()와 equals()를 오버라이딩 한다. 그리고 작성해주면 된다. 

2.2 TreeSet

 

3. Map

3.1 HashMap

3.2 Hashtable

3.3 TreeMap

3.4 Properties

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함