treeMap 基於紅黑樹實現,treeSet基於treeMap實現,排序
當咱們修改默認的排序規則時候,必須有0的返回,不然可能刪除不掉元素rem
public class TreeSet01 { public static void main(String[] args) { TreeSet<Integer> kminset = new TreeSet<Integer>(); kminset.add(1); kminset.add(2); kminset.remove(kminset.first().intValue()); System.out.println("kminset size = [" + kminset.size() + "]"); // 1 TreeSet<Integer> kminset0 = new TreeSet<Integer>(new MyComparator()); kminset0.add(1); kminset0.add(2); kminset0.remove(kminset0.first().intValue()); System.out.println("kminset0 size = [" + kminset0.size() + "]"); //2 } //必須有 0, >0, <0 三個值得返回 static class MyComparator implements Comparator<Integer> { public int compare(Integer f1,Integer f2) { if(f1.intValue()== f2.intValue()){ return 0; } if (f1.intValue()> f2.intValue()) { return 1; } return -1; } } }