코딩테스트를 위한 자바 메소드 정리(3) - HashSet

Jae·2023년 7월 7일
0

HashSet

  • Set 인터페이스를 구현한 대표적인 컬렉션이며 데이터의 저장순서가 유지되지 않고 중복을 허용하지 않는다. 저장 순서를 유지하고 싶다면 LinkedHashSet을 사용해야 한다.

생성자

  • HashSet() : HashSet 객체를 생성
  • HashSet(Collection c) : 주어진 컬렉션을 포함하는 HashSet 객체 생성
  • HashSet(int intitialCapacity) : 주어진 값을 초기용량으로 하는 HashSet 객체 생성
  • HashSet(int intitialCapacity, float loadFactor) : 초기 용량과 load factor를 지정하는 생성자

🚨 load factor 란 ?
저장공간이 가득 차기 전에 미리 용량을 확보하기 위한 것이다.
ex ) load factor : 0.7 -> 저장공간의 70%가 찼을 때 용량이 2배로 늘어남
지정하지 않았을 때의 기본 값은 75% -> load factor : 0.75


메소드

  • boolean add (Object obj) : 새로운 객체를 저장
  • boolean addAll(Collection c) : 주어진 컬렉션에 저장된 모든 객체들을 추가
Set HashSet1 = new HashSet();
// HashSet은 중복 불가이기 때문에 중복된 노드들은 제거 된다. 
HashSet1.add(1);    // Integer 타입의 숫자 1
HashSet1.add("1"); // 문자열 1
HashSet1.add("1"); // 문자열 1
HashSet1.add("2"); // 문자열 2
HashSet1.add("2"); // 문자열 2

System.out.println(HashSet1) ;  // [1,1,2] -> 숫자 1과 문자 1은 타입이 다르기 때문에 제거 x


Set HashSet2 = new HashSet();
HashSet2.add("1");
HashSet2.add("2");
HashSet2.add("3");
 
HashSet2.addAll(HashSet1);  // Set은 중복이 불가 -> HashSet1의 노드중 HashSet2의 노드와 동일한 값이 있으면 제거된다.

System.out.println(HashSet2);  // [1,1,2,3]
  • void clear() : 저장된 모든 객체 삭제
Set HashSet1 = new HashSet();

HashSet1.add(1);
HashSet1.add(2);

System.out.println(HashSet1); // [1,2]

HashSet1.clear();

System.out.println(HashSet1); // []

  • Object clone() : HashSet을 복사해서 반환한다.(얕은 복사)
Set HashSet1 = new HashSet();

HashSet1.add(1);
HashSet1.add(2);

System.out.println(HashSet1); // [1,2]

Set HashSet2 = (HashSet) HashSet1.clone();

System.out.println(HashSet2); // [1,2]
  • boolean contains(Object obj) : 지정한 객체를 포함하고 있는지 알려준다.
  • boolean containsAll(Collection c) : 주어진 컬렉션에 저장된 모든 객체를 포함하고 있는지 알려준다.
Set HashSet1 = new HashSet();
HashSet1.add(1);
HashSet1.add(2);
System.out.println(HashSet1.contains(3));  // false

Set HashSet2 = new HashSet();
HashSet2.add(1);
HashSet2.add(2);
System.out.println(HashSet2.containsAll(HashSet1));  // true
  • boolean isEmpty() : HashSet이 비었는지 알려준다 .
Set HashSet1 = new HashSet();
HashSet1.add(1);
HashSet1.add(2);
System.out.println(HashSet1.isEmpty());  // false

Set HashSet2 = new HashSet();
System.out.println(HashSet2.isEmpty()); // true
  • Iterator iterator() : iterator 반환
Set HashSet1 = new HashSet();
HashSet1.add(1);
HashSet1.add(2);

Iterator it = HashSet1.iterator();

while(it.hasNext()){
System.out.print(it.next());    // 1 2
}
  • boolean remove(Object obj) : 지정된 객체 HashSet에서 삭제 -> 성공하면 true, 실패하면 false 반환
  • boolean removeAll(Collection c) : 주어진 컬렉션에 저장된 모든 객체와 동일한 것을 HashSet에서 삭제 (차집합) -> 성공하면 true, 실패하면 false 반환
Set HashSet1 = new HashSet();
HashSet1.add(1);
HashSet1.add(2);

HashSet1.remove(1);  // true
HashSet1.remove(3); // false 

System.out.println(HashSet1);   // [2]

Set HashSet2 = new HashSet();
HashSet2.add(1);
HashSet2.add(2);
HashSet2.add(3);

HashSet2.removeAll(HashSet1) ; // 하나라도 삭제되면 true 
System.out.println(HashSet2);  // [2,3]


  • boolean retainAll(Collection c) : 주어진 컬렉션에 저장된 객체와 동일한 것만 남기고 삭제 (교집합)
Set HashSet1 = new HashSet();
HashSet1.add(1);
HashSet1.add(2);
HashSet1.add(3);

Set HashSet2 = new HashSet();
HashSet2.add(1);
HashSet2.add(2);
HashSet2.add(4);

HashSet2.retainAll(HashSet1); // 일치하는 1,2 외에 다 삭제 
System.out.println(HashSet2);  // [1,2]



  • int size() : 저장된 객체의 개수 반환
Set HashSet1 = new HashSet();
HashSet1.add(1);
HashSet1.add(2);
HashSet1.add(3);

System.out.println(HashSet1.size());  // 3

  • Object[] toArray() : 저장된 객체들을 객체배열의 형태로 반환
  • Object[] toArray(Object[] arr) : 저장된 객체들을 주어진 객체배열(arr)에 담는다.
Set HashSet1 = new HashSet();
HashSet1.add(1);
HashSet1.add(2);
HashSet1.add(3);

Object[] objArr = HashSet1.toArray();

Set HashSet2 = new HashSet();
HashSet2.add(1);
HashSet2.add(2);
HashSet2.add(3);

Object[] Arr = new Object[HashSet2.size()];
Object objArr = HashSet2.toArray(Arr);

profile
Back-end Developer

0개의 댓글