2021.05.07
OOOXXX
- OOO : 자료구조(물리적인 공간의 형태)
- XXX : 인터페이스(사용법)
List
- ArrayList : Array(배열 형태의 공간) + List(리스트 사용법제공,index)
- LinkedList : Linked(링크 형태의 공간) + List(리스트 사용법제공,index)
Set
- HashSet : Hash(알고리즘 사용, 구조) + Set(사용법, 중복값X)
- TreeSet : Tree(트리 구조) + Set(사용법, 중복값X)
Map
- HashMap : Hash(알고리즘) + Map(key,value)
- TreeMap : Tree(트리구조) + Map(key,value)
TreeSet
- Tree 구조를 가지는 Set
- 중복값 X, 순서(index) X, 정렬 X
- 이진 검색 트리 구조 기반의 자동 정렬이 되어있는 Set
- 정렬 필요 + 중복값 X + 검색 자주
public class Ex78_TreeSet {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<Integer>();
set.add(10);
set.add(40);
set.add(20);
set.add(50);
set.add(30);
set.add(10);
System.out.println(set.size());
System.out.println();
Iterator<Integer> iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
System.out.println();
System.out.println(set.first());
System.out.println(set.last());
System.out.println(set.headSet(30));
System.out.println(set.tailSet(30));
System.out.println(set.subSet(30, 50));
ArrayList<Score> list = new ArrayList<Score>();
list.add(new Score("홍길동", 100, 90, 80));
list.add(new Score("아무개", 99, 88, 77));
list.add(new Score("하하하", 81, 69, 87));
System.out.println(list);
list.sort(new Comparator<Score>() {
@Override
public int compare(Score o1, Score o2) {
return (o1.getKor() + o1.getEng() + o1.getMath()) - (o2.getKor() + o2.getEng() + o2.getMath());
}
});
System.out.println(list);
System.out.println();
HashSet<Score> hset = new HashSet<Score>();
hset.add(new Score("홍길동", 100, 90, 80));
hset.add(new Score("아무개", 99, 88, 77));
hset.add(new Score("하하하", 81, 69, 87));
System.out.println(hset);
hset.add(new Score("하하하", 81, 69, 87));
System.out.println(hset);
System.out.println();
TreeSet<Score> tset = new TreeSet<Score>();
tset.add(new Score("홍길동", 100, 90, 80));
tset.add(new Score("아무개", 99, 88, 77));
tset.add(new Score("하하하", 81, 69, 87));
System.out.println(tset);
}
}
class Score implements Comparable<Score> {
private String name;
private int kor;
private int eng;
private int math;
public Score(String name, int kor, int eng, int math) {
super();
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
@Override
public String toString() {
return "Score [name=" + name + ", kor=" + kor + ", eng=" + eng + ", math=" + math + "]";
}
@Override
public int hashCode() {
return (this.name + this.kor + this.eng + this.math).hashCode();
}
@Override
public boolean equals(Object obj) {
return this.hashCode() == obj.hashCode();
}
@Override
public int compareTo(Score o) {
return this.getName().compareTo(o.getName());
}
}
treeMap
- Tree -> 이진 트리 구조
- Map -> key, value
- 키를 자동정렬
- 키: 이진 트리
- 값: 리스트
public class Ex79_TreeMap {
public static void main(String[] args) {
TreeMap<String,String> map = new TreeMap<String,String>();
map.put("one", "하나");
map.put("two", "둘");
map.put("three", "셋");
map.put("four", "넷");
map.put("five", "다섯");
System.out.println(map.size());
System.out.println(map);
System.out.println(map.get("three"));
System.out.println(map.firstKey());
System.out.println(map.lastKey());
System.out.println(map.firstEntry());
System.out.println(map.lastEntry());
System.out.println(map.headMap("o"));
System.out.println(map.tailMap("o"));
System.out.println(map.subMap("f", "o"));
}
}