
LinkedHashSet은 그림과 같이 HashSet을 상속한다. LinkedHashSet은 HashSet과는 달리 저장순서를 유지하는 기능이 추가되어 있다. 이는 두 컬렉션의 내부구현 방식의 차이에 기인하는데 HashSet은 내부적으로 HashMap컬렉션을 사용하여 요소들을 저장하는 반면, LinkedHashSet은 내부적으로 이중연결리스트(Doubly Linked List)를 사용하여 요소들을 저장한다.
LinkedHashSet은 HashSet의 메서드를 그대로 사용하며 자체적으로 추가된 메서드는 없다.
반환타입이 boolean인 메서드들은 작업에 성공하거나 사실이면 true, 아니면 false를 반환한다.
| 메서드 | 기능 |
|---|---|
| LinkedHashSet() | 초기용량이 16이고 loadfactor가 0.75인 빈 LinkedHashSet 객체를 생성 |
| LinkedHashSet(Collection c) | 주어진 컬렉션을 포함하는 LinkedHashSet 객체를 생성 |
| LinkedHashSet(int initialCapacity) | 주어진 값을 초기용량으로 하는 LinkedHashSet 객체를 생성 |
| LinkedHashSet(int initalCapacity, float loadFactor) | 초기용량과 load factor를 지정하는 생성자 |
| 메서드 | 기능 |
|---|---|
| boolean add(Object o) boolean addAll(Collection c) | 지정된 객체(o) 또는 Collection(c)의 객체들을 LinkedHashSet에 추가. |
| 메서드 | 기능 |
|---|---|
| boolean contains(Object o) boolean containsAll(Collection c) | 지정된 객체(o) 또는 Collection(c)의 객체들이 포함되어 있는지 확인. |
| 메서드 | 기능 |
|---|---|
| boolean remove(Object o) boolean removeAll(Collection c) | 지정된 객체(o) 또는 Collection(c)의 객체들을 삭제. |
| boolean retainAll(Collection c) | 지정된 Collection에 포함된 객체만을 남기고, 다른 객체들은 LinkedHashSet에서 삭제 집합의 교집합 개념과 유사함. |
| void clear() | LinkedHashSet의 모든 객체를 삭제 |
| 메서드 | 기능 |
|---|---|
| boolean equals(Object o) | 동일한 LinkedHashSet인지 확인 |
| int hashCode() | LinkedHashSett의 해시 코드를 반환 |
| Object clone() | LinkedHashSet을 복제해서 반환 (얕은 복사) |
| 메서드 | 기능 |
|---|---|
| Iterator iterator() | 해당 LinkedHashSet의 iterator를 얻어서 반환 |
| 메서드 | 기능 |
|---|---|
| boolean isEmpty() | LinkedHashSet이 비어있는지 확인 |
| int size() | LinkedHashSet에 저장된 객체의 개수를 반환 |
| 메서드 | 기능 |
|---|---|
| Object[] toArray() | LinkedHashSet에 저장된 객체를 객체배열(Object[])로 반환 |
| Object[] toArray(Object[] a) | 지정된 배열에 LinkedHashSet의 객체를 저장해서 반환 |
자바의 정석 3판 (저자 : 남궁성)
Java Collections Framework Video Tutorial
오라클 Java 문서