728x90
반응형
package chap13;
import java.util.HashSet;
import java.util.Set;
/*
* Collection 인터페이스 : 객체들의 모임
* Set 인터페이스 : Collection 인터페이스의 하위 인터페이스
* 중복 저장 불가. 순서 유지 안함.
* 구현클래스 : HashSet, TreeSet, LinkedHashSet
*/
public class SetEx1 {
public static void main(String[] args) {
Object[] arr = {"홍길동",1,"1","김삿갓","이몽룡","홍길동","성춘향","향단이","홍길동","김삿갓"};
Set<Object> set1 = new HashSet<>();
Set<Object> set2 = new HashSet<>();
Set<Object> set3 = new HashSet<>();
for(Object o : arr) {
//boolean add(o) : true => 추가완료.
// false => 추가실패
if(!set1.add(o)) {
if(!set2.add(o)) {
set3.add(o);
}
}
}
System.out.println(set1);
System.out.println(set2);
System.out.println(set3);
}
}
728x90
반응형
'study > Java' 카테고리의 다른 글
[Java] 32. Collection (Set) Exam2 (Student 클래스 구현하기 ) (0) | 2022.03.30 |
---|---|
[Java] 32. Collection (Set2) (0) | 2022.03.30 |
[Java] 32. Collection (List3) (0) | 2022.03.30 |
[Java] 32. Collection (List2) (0) | 2022.03.30 |
[Java] 32. Collection (List) Exam1 (화면에서 홀수개의 정수를 입력받아 입력받은 숫자의 평균과 중간값 출력하기) (0) | 2022.03.30 |