🖥️ Backend/Java

HashSet 클래스 메서드 총 정리

Developer Quarterly 2025. 5. 14. 18:50

Hierarchy of Java Collections

HashSet 클래스 선언부
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable

 

HashSet 클래스는 Set 인터페이스에 있는 모든 추상 메서드를 오버라이딩하고 있다. 그 중 주요 메서드만 설명하면 아래와 같다.

메서드/설명 예제 결과
import java.util.HashSet;
HashSet()
HashSet을 생성한다.
HashSet<String> set = new HashSet<>();
System.out.println(set);
[]
HashSet(Collection<? extends E> c)
컬렉션 c에 포함된 요소들을 이용해 새로운 HashSet을 생성하고 초기화한다.
List<String> list = Arrays.asList("A", "B", "C");
HashSet<String> set = new HashSet<>(list);
System.out.println(set);
[A, B, C]
(순서는 보장되지 않는다)
HashSet(int capacity)
주어진 정수 값 capacity를 초기 용량으로 설정하여 새로운 HashSet을 생성하고 초기화한다.
요소가 HashSet에 추가됨에 따라 용량은 자동으로 증가한다.
HashSet<String> set = new HashSet<>(50);
set.add("Apple");
System.out.println(set);
[Apple]
HashSet(int capacity, float loadFactor)
주어진 정수 값 capacity와 지정된 로드 팩터(load factor)를 초기 설정으로 하여 새로운 HashSet을 생성하고 초기화한다.
HashSet<String> set = new HashSet<>(100, 0.75f);
set.add("Banana");
System.out.println(set);
[Banana]
boolean isEmpty()
집합이 비어 있으면 true를 반환한다.
HashSet<String> set = new HashSet<>();
System.out.println(set.isEmpty());
set.add("A");
System.out.println(set.isEmpty());
true  
false
int size()
집합에 포함된 요소의 개수를 반환한다.
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
System.out.println(set.size());
2
Object[] toArray()
저장된 객체를 객체배열(Object[ ])로 반환한다.
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
Object[] array = set.toArray();
System.out.println(Arrays.toString(array));
[A, B]
(순서는 무작위)
Object[] toArray(Object[] a)
저장된 객체들을 지정된 객체배열의 형태로 반환한다.
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
String[] array = set.toArray(new String[0]);
System.out.println(Arrays.toString(array));
[A, B]
(순서는 무작위)
boolean add(Object o)
지정된 객체(o)를 추가한다.
(이미 존재하면 추가되지 않고 false 반환)
HashSet<String> set = new HashSet<>();
System.out.println(set.add("A"));
System.out.println(set.add("A"));
System.out.println(set);
true  
false  
[A]
boolean addAll(Collection c)
주어진 컬렉션의 모든 요소를 HashSet에 추가한다.(합집합)
HashSet<String> set = new HashSet<>();
List<String> list = Arrays.asList("A", "B", "C");
set.addAll(list);
System.out.println(set);
[A, B, C]
boolean remove(Object o)
지정된 객체를 삭제한다.
HashSet<String> set = new HashSet<>();
set.add("A");
System.out.println(set.remove("A"));
System.out.println(set.remove("B"));
System.out.println(set);
true  
false  
[]
boolean removeAll(Collection c)
주어진 컬렉션에 포함된 모든 객체를 HashSet에서 제거한다.(차집합)
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("C");
List<String> list = Arrays.asList("A", "C");
set.removeAll(list);
System.out.println(set);
[B]
boolean retainAll(Collection c)
주어진 컬렉션에 포함된 객체만 남기고 나머지는 모두 HashSet에서 제거한다.(교집합)
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("C");
List<String> list = Arrays.asList("B", "D");
set.retainAll(list);
System.out.println(set);
[B]
void clear()
HashSet의 모든 객체를 삭제한다.
HashSet<String> set = new HashSet<>();
set.add("X");
set.add("Y");
set.clear();
System.out.println(set);
[]
boolean contains(Object o)
지정된 객체(o)가 HashSet에 포함되어 있는지 확인한다.
HashSet<String> set = new HashSet<>();
set.add("Apple");
System.out.println(set.contains("Apple"));
System.out.println(set.contains("Banana"));
true  
false
boolean containsAll(Collection c)
주어진 컬렉션에 포함된 모든 객체가 HashSet에도 모두 포함되어 있는지 확인한다.(부분집합)
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
set.add("C");
List<String> list = Arrays.asList("A", "B");
System.out.println(set.containsAll(list));
true
Iterator iterator()
HashSet의 요소를 순회할 수 있는 Iterator를 반환한다.
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
Iterator<String> it = set.iterator();
while (it.hasNext()) {
    System.out.print(it.next() + " ");
}
A B
(※ 출력 순서는 HashSet 특성상 보장되지 않는다)