import java.util.*;
public class SetTest001 {
public static void main(String[] args) {
// Set : 중복을 허용하지 않는 자료구조 인터페이스
// HashSet(순서무시), LinkedHashSet(순서유지) 은 정렬을 지원하지 않는다.
// TreeSet은 정렬을 지원한다.
Set<String> hashSet = new HashSet<String>();
hashSet.add("사과");
hashSet.add("딸기");
hashSet.add("배");
hashSet.add("복숭아");
System.out.println(hashSet.size() + "개의 데이터가 있습니다.");
System.out.println(hashSet);
hashSet.add("배");
hashSet.add("복숭아");
System.out.println(hashSet.size() + "개의 데이터가 있습니다.");
System.out.println(hashSet);
//-----------------------------------------------------------------------------
Set<String> linkedHashSet = new LinkedHashSet<String>();
linkedHashSet.add("사과");
linkedHashSet.add("딸기");
linkedHashSet.add("배");
linkedHashSet.add("복숭아");
System.out.println(linkedHashSet.size() + "개의 데이터가 있습니다.");
System.out.println(linkedHashSet);
linkedHashSet.add("배");
linkedHashSet.add("복숭아");
System.out.println(linkedHashSet.size() + "개의 데이터가 있습니다.");
System.out.println(linkedHashSet);
//-----------------------------------------------------------------------------
Set<Integer> lotto = new LinkedHashSet<>(); // JDK 1.7부터는 뒤에 <>로만 표시 가능하다.
Random rnd = new Random();
while(lotto.size()<6) lotto.add(rnd.nextInt(45)+1);
System.out.println(lotto);
//-----------------------------------------------------------------------------
Set<Integer> lotto2 = new TreeSet<>(); // JDK 1.7부터는 뒤에 <>로만 표시 가능하다.
while(lotto2.size()<6) lotto2.add(rnd.nextInt(45)+1);
System.out.println(lotto2);
}
}
댓글 없음:
댓글 쓰기