LinkedHashSet
을 사용해야 한다. 🚨
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
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);