
leaf 노드 : 자식에 대한 참조가 null인 경우
import java.util.Comparator;
public class FruitComparator implements Comparator<Fruit> {
@Override
public int compare(Fruit o1, Fruit o2) {
if(o1.price < o2.price) return -1;
/* return o1.price - o2.price; // int
return o1.name.compareTo(o2.name); // 과일명 */
else if(o1.price == o2.price) return 0;
else return 1;
}
}
import java.util.TreeSet;
public class ComparatorExample {
public static void main(String[] args) {
//비교자를 제공한 TreeSet 컬렉션 생성
TreeSet<Fruit> treeSet = new TreeSet<Fruit>(new FruitComparator());
/* 익명 구현객체
new Comparator() {compare() {}}
price와 fruit으로 내보내보기 */
//객체 저장
treeSet.add(new Fruit("포도", 3000));
treeSet.add(new Fruit("수박", 10000));
treeSet.add(new Fruit("딸기", 6000));
//객체를 하나씩 가져오기
for(Fruit fruit : treeSet) {
System.out.println(fruit.name + ":" + fruit.price);
}
}
}
짝이 맞을 경우
1. pop할 게 없는 경우
2. 스택이 비어있는 경우
SOLID
OCP 위배
배열을 리스트로 변경 : ArrayList - static 메서드, 변경불가 list
변경가능한 list를 만들고 싶으면, new ArrayList()로 만들기
리스트를 배열을 변경 : toArray()로 만들기
Map -> List로 만들고 정렬하기 : Entry<k,v> 활용하기