TreeNode
HashSet
보다 데이터 추가, 삭제에 시간이 더 걸림(반복적인 비교 후 저장)사실 표현하면
boolean add(Object o)
compare()
를 호출해서 비교TreeSet()
comparaeTo()
을 사용하여 비교한다.1) 💖💖 클래스 implements Comparable{}
2) 💖💖 public int compareTo(Object o)
로 구현부 완성
0
: 어떤 걸 add해도 하나만 생성됨1 :
->
[Test@1f32e575, Test@279f2327]
-1 :
->
[Test@1f32e575, Test@279f2327]
0 :
->
[Test@279f2327]
TreeSet(Collectoin c)
TreeSet(Comparator comp)
1) 💖💖 클래스 implements Comparator{}
2) 💖💖 public int compare(Object o1, Object o2)
로 구현부 완성
0
: 어떤 걸 add해도 하나만 생성됨3) 💖✨ 선언 : TreeSet(new 클래스명())
1 :
->
[TestCom@28a418fc, TestCom@5305068a, 1z, 1]
-1 :
->
[1, 1z, TestCom@28a418fc, TestCom@5305068a]
0 :
->
[TestCom@28a418fc]
boolean add(Object o)
compare()
를 호출해서 비교Object first()
Object last()
Object ceiling(Object o)
Object floor(Object o)
Object higher(Object o)
Object lower(Object o)
SortedSet subSet(Object fromElement, Object toElement)
SortedSet headSet(Object toElement)
SortedSet tailSet(Object fromElement)
ex11_13
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class Ex11_13 {
public static void main(String[] args) {
Set setHash = new HashSet();
Set setTree = new TreeSet();
for(int i=0; setHash.size()<6; i++) {
int num = (int)(Math.random()*45 +1); //1<= x <46
setHash.add(new Integer(num)); //setHash.add(num)도 가능
}
System.out.println("=== 기본 HashSet ===");
System.out.println(setHash); //정렬X
//정렬하려면 list에 저장하기!!
System.out.println("=== 정렬 후 HashSet ===");
List list = new LinkedList(setHash);
Collections.sort(list);
System.out.println(list);
for(int i=0; setTree.size()<6; i++) {
int num = (int)(Math.random()*45 +1); //1<= x <46
setTree.add(new Integer(num)); //setHash.add(num)도 가능
}
System.out.println("=== 이미 정렬된 TreeSet ===");
System.out.println(setTree); //정렬O
//정렬이 따로 해줄 필요가 없다!
Set set = new TreeSet(new TestCom()); //비교기준을 넣어야함
set.add(new TestCom());
set.add(new TestCom());
set.add("1z");
set.add(new Integer(1));
System.out.println(set);
Set set2 = new TreeSet();
set2.add(new Test());
set2.add(new Test());
// set2.add("1z"); 신기하게 이런것들은 안되나보다
// set2.add(new Integer(1));
System.out.println(set2);
}
}
class Test implements Comparable{
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}
}
class TestCom implements Comparator{
@Override
public int compare(Object o1, Object o2) {
// TODO Auto-generated method stub
return 1;
}
}
=== 기본 HashSet ===
[1, 18, 36, 4, 10, 11]
=== 정렬 후 HashSet ===
[1, 4, 10, 11, 18, 36]
=== 이미 정렬된 TreeSet ===
[8, 18, 21, 27, 34, 41]
[TestCom@28a418fc, TestCom@5305068a, 1z, 1]
[Test@279f2327]
ex11_14
import java.util.TreeSet;
public class Ex11_14 {
public static void main(String[] args) {
TreeSet set = new TreeSet();
String from = "b";
String to = "d";
set.add("abc"); set.add("alien"); set.add("bat");
set.add("car"); set.add("Car"); set.add("disc");
set.add("dance"); set.add("dZZZZ"); set.add("dzzz");
set.add("elephant"); set.add("elevator"); set.add("fan");
set.add("flower"); set.add("dzzy"); set.add("dzzzz");
set.add("dzzY");
System.out.println(set);
System.out.println("range search : "+from+" to "+to);
System.out.println("result1 : "+set.subSet(from, to)); //b~c
System.out.println("range search : "+from+" to "+to+"zzy");
System.out.println("result2 : "+set.subSet(from, to+"zzy"));
System.out.println("range search : "+from+" to "+to+"zzzY");
System.out.println("result3 : "+set.subSet(from, to+"zzzY"));
System.out.println("range search : "+from+" to "+to+"zzzz");
System.out.println("result4 : "+set.subSet(from, to+"zzzz"));
}
}
[Car, abc, alien, bat, car, dZZZZ, dance, disc, dzzY, dzzy, dzzz, dzzzz, elephant, elevator, fan, flower]
range search : b to d
result1 : [bat, car]
range search : b to dzzy
result2 : [bat, car, dZZZZ, dance, disc, dzzY]
range search : b to dzzzY
result3 : [bat, car, dZZZZ, dance, disc, dzzY, dzzy, dzzz]
range search : b to dzzzz
result4 : [bat, car, dZZZZ, dance, disc, dzzY, dzzy, dzzz]
ex11_15
import java.util.TreeSet;
public class Ex11_15 {
public static void main(String[] args) {
TreeSet set = new TreeSet();
int[] score = {80,95,50,35,45,65,10,100};
for(int i=0; i<score.length; i++) {
set.add(new Integer(score[i]));
}
System.out.println("50보다 작은 값 : "+set.headSet(new Integer(50)));
System.out.println("50보다 큰 값 : "+set.tailSet(new Integer(50)));
System.out.println("40과 80사이의 값 : "+set.subSet(new Integer(40), new Integer(80)));
}
}
50보다 작은 값 : [10, 35, 45]
50보다 큰 값 : [50, 65, 80, 95, 100]
40과 80사이의 값 : [45, 50, 65]
headSet, tailSet, subSet 은 TreeSet에만 있는 메소드이기 때문이다!!