💁♀️ 셋(Set)이란,
저장 순서가 유지되지 않고, 중복 인스턴스도 저장하지 못하게 하는 자료구조
(null값도 중복하지 않게 하나의 null만 저장)
- List와 완전히 반대의 특징
📍 Set 계열
1) HashSet
2) LinkedHashSet
3) TreeSet
💁♀️ Set 계열에 모두 적용되는 메소드
💁♀️ HashSet이란,
Set 컬렉션 클래스에서 가장 많이 사용되는 클래스중 하나.
Set에 인스턴스를 저장할 때 hash함수를 사용하여 처리 속도가 빠름.
동일 인스턴스 뿐 아니라 동등 인스턴스도 중복하여 저장하지 않음
- 동일 인스턴스 : 완전히 같은 인스턴스
- 동등 인스턴스 : 다른 인스턴스이지만 특정한 기준들의 속성 값이 같음
[1] HashSet 인스턴스 생성 후 요소 추가 (add)
Set<String> hset = new HashSet<>();
hset.add(new String("java"));
hset.add(new String("oracle"));
hset.add(new String("jdbc"));
hset.add(new String("html"));
hset.add(new String("css"));
[2] 저장 순서가 유지되지 않음
System.out.println(hset);
>>> 입력한 순서가 아닌, HashSet 본인만의 기준으로 연산결과를 출력
[3] 값이 같은 동등 객체를 저장하고자 했을 때 '중복 저장되지 않음'
hset.add(new String("java"));
System.out.println(hset);
[4] 값이 같은 동등 객체를 이용해 포함 여부 확인이 가능 (contains)
System.out.println("포함 여부 확인 : " + hset.contains(new String("oracle")));
// true 출력
[5] 저장된 객체를 하나씩 꺼내는 기능이 따로 없기 때문에 '반복문(인덱스) 사용이 불가'
>>> [5-1] toArray()를 이용하여 배열로 변경한 뒤 for문 사용
Object[] arr = hset.toArray();
for(int i = 0; i < arr.length; i++) {
System.out.println(i + " " + arr[i]);
}
// [출력문]
// 0 css
// 1 java
// 2 oracle
// 3 jdbc
// 4 html
>>> [5-2] iterator()로 목록 만들어 연속 처리
Iterator<String> iter = hset.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
} // 위의 출력문과 동일하게 출력됨
>>> [5-3] 향상된 for문은 사용 가능
for(String str : hset) {
System.out.println("str : " + str);
public class BookDTO implements Comparable<BookDTO>{
private int number;
private String title;
private String author;
private int price;
public BookDTO() {}
public BookDTO(int number, String title, String author, int price) {
super();
this.number = number;
this.title = title;
this.author = author;
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "BookDTO [number=" + number + ", title=" + title + ", author=" + author + ", price=" + price + "]";
}
@Override
public int compareTo(BookDTO o) {
>>> this와 비교 대상 객체 BookDTO o를 비교하여 number 오름차순 정렬
return number - o.number;
}
@Override
>>> public int hashCode() {
return Objects.hash(author, number, price, title);
}
@Override
>>> public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BookDTO other = (BookDTO) obj;
return Objects.equals(author, other.author) && number == other.number && price == other.price
&& Objects.equals(title, other.title);
}
}
// HashSet에 BookDTO 저장
Set<BookDTO> hset2 = new HashSet<>();
// 동등 객체 중복 저장 테스트
hset2.add(new BookDTO(1, "홍길동전", "허균", 50000));
hset2.add(new BookDTO(1, "홍길동전", "허균", 50000));
System.out.println(hset2);
// 동등 객체 포함 여부 테스트
System.out.println(hset2.contains(new BookDTO(1, "홍길동전", "허균", 50000)));
>>> String 클래스 내에 hashCode, equals 메소드가 오버라이딩 되어 동등 객체 처리가 가능했던
것이고, 직접 구현한 클래스에서 동일한 처리가 될 수 있도록 하려면 hashCode, equals
메소드를 오버라이딩 해야함
* 오버라이딩 되어있지 않을 경우, 동등 객체를 중복 저장하지않고 동등 객체 포함 여부 확인
가능한 HashSet인데도 불구하고 동등 객체 인식을 하지 못 해 출력문 및 fasle 값을 출력
💁♀️ LinkedHashSet이란,
HashSet이 가지고 있는 기능을 모두 가지고 있고 추가적으로 저장 순서를 유지하는 특징을 가지고 있음
Set<String> lhset = new LinkedHashSet<>();
lhset.add("java");
lhset.add("oracle");
lhset.add("jdbc");
lhset.add("html");
lhset.add("css");
System.out.println(lhset);
>>> HashSet과는 달리, 입력한 순서대로 출력
💁♀️ TreeSet이란,
데이터가 정렬된 상태로 저장되는 이진 검색 트리의 형태로 요소를 저장
(이진 검색 트리는 데이터를 추가하거나 삭제하는 등의 기본 동작 시간이 매우 빠름)
- Set 인터페이스가 가지는 특징을 그대로 가지지만 정렬 된 상태를 유지한다는 점이 다른 점 (정렬 메소드를 사용하지 않아도 오름차순 정렬)
Set<String> tset = new TreeSet<>();
tset.add("java");
tset.add("oracle");
tset.add("jdbc");
tset.add("html");
tset.add("css");
System.out.println(tset);
>>> 저장 순서와 상관없이 오름차순 정렬된 상태로 출력
Set<Integer> lotto = new TreeSet<>();
while(lotto.size() < 6){
lotto.add((int)(Math.random() * 45) + 1);
}
System.out.println(lotto);
>>> 6개 자리까지 중복되는 숫자없이 계속 숫자가 돌아가고 정렬된 상태로 출력