객체를 중복해서 저장할 수 없으며, 하나의 null 값만 저장할 수 있다.
중복을 자동으로 제거해준다.

<출처 : 자바 HashSet 사용법 & 예제 총정리 >
Set은 비선형 구조이기 때문에 '순서'의 개념과 '인덱스'가 존재하지 않는다.
때문에 값을 추가 / 삭제 하는 경우 Set 내부에 해당 값을 검색하여 해당 기능을 수행해야 한다. 이로 인해 처리 속도가 List구조에 느리다는 것이 단점이다.
HashSet은 Set 인터페이스에서 지원하는 구현 클래스이다. 때문에 Set의 성질을 그대로 상속받는 다는 것이 특징이다.
값의 존재 유무를 파악할 때 사용할 수 있다.
저장 순서를 유지해야 한다면 JDK 1.4부터 제공되는 클래스
LinkedHashSet을 사용해야 한다.
HashSet 내부 코드를 보면 HashMap을 사용하여 구현되어 있는 것을 볼 수 있다.
아래 코드와 같이 사용하여 다양한 방법으로 Hash를 생성할 수 있다.
// 타입을 지정 가능
HashSet<String> animals1 = new HashSet<String>();
// 타입을 생략하여 사용 가능 -> 빈 HashSet생성 시 사용
HashSet<String> animals2 = new HashSet<>();
// 초기 용량(Capacity) 설정
HashSet<String> animals3 = new HashSet<>(10);
// animal의 모든 값을 가진 HashSet 생성
HashSet<String> animals4 = new HashSet<>(animals1);
//초기값 지정 가능
HashSet<String> animals5 = new HashSet<>(Arrays.asList("tiger", "lion", "fox"));
생성된 Hash에 add() 메소드를 호출하여 값을 추가 가능하다.
HashSet<String> animals = new HashSet<>()
animals.add("tiger");
animals.add("lion");
animals.add("fox");
HashSet은 저장 순서가 보장되지 않기에 특정 위치에 값을 추가하거나 할 수는 없다는 것을 명심 !
만약 입력되는 값이
size() 메소드를 사용하여 Hash의 크기를 구할 수 있다.
HashSet<Integer> set = new HashSet<Integer>(Arrays.asList(1,2,3));
//set 크기 : 3
System.out.println(set.size());
remove(value) 와 clear() 메소드를 사용하여 Hash값을 제거할 수 있다.
HashSet<Integer> set = new HashSet<Integer>(Arrays.asList(1,2,3));
//값 1 제거
set.remove(2);
//모든 값을 제거
set.clear();
만약 삭제하려는 값이
원하는 값에 대해 contains(value) 메소드를 통해 Hash 내부에 존재하는 지 확인이 가능하다.
HashSet<Integer> set = new HashSet<Integer>(Arrays.asList(1,2,3));
////set내부에 값 1이 있다면 true 출력, 없다면 false 출력
System.out.println(set.contains(1));
Set 컬렉션을 그냥 'print' 처리 할 경우 대괄호( '[ ]' )로 묶여 Set의 전체값이 출력된다.
때문에, 전체 객체를 대상으로 한번씩 반복해서 가져오는 반복자 (Iterator)를 사용해 출력해야 한다.
HashSet<Integer> set = new HashSet<Integer>(Arrays.asList(1,2,3));
//출력결과 : [1,2,3]
System.out.println(set);
Iterator iter = set.iterator();
//hasNext() : 가져올 객체가 있다면 true 리턴, 없다면 false 리턴
// next() : Iterator에서 하나의 객체를 가져올 수 있는 메소드
while(iter.hasNext()) {
System.out.println(iter.next());
}
📚 Reference
HashSet이란? (tistory | crazykim2)
자바 HashSet 사용 방법 (tistory | psychoria )
I feel a lot more people need to read this, very good info!
[url=https://youtu.be/UcCSp0_-Kcc]Home Remedy to Get Rid of Mic[/url]
Bamboo plywood is especially interesting in those applications where the side of plywood remains visible, like steps of a staircase and kitchen work tops. Havana Bamboo Plywood
Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!