????3?????????????????????????????????????????????????????????????????????????????????Щ????????????????????????????????????·?????????????????????????????????β?????????“?β???”???????????????????????????????????????????????Java??TreeMap???????????????

????1??????????????ü???????Comparable<T>?????????????TreeMap?????????????y?????compareTo()?????????ж?ü??????????????????λ?á??????′???

public class MyTest implements Comparable<MyTest> {
         private int key;
         public MyTest(int key) {
             this.key = key;
         }
         @Override
         public boolean equals(Object o) {
             if (!(o instanceof MyTest))
                 return false;
             MyTest t = (MyTest)o;
             System.out.println("equals of MyTest is called here.");
             return key == t.key;
         }
         @Override
         public int compareTo(MyTest o) {
             return key - o.key;
         }
         public static void main(String[] args) {
             TreeMap<MyTest??Integer> tm = new TreeMap<MyTest??Integer>();
             tm.put(new MyTest(5)?? 5);
             tm.put(new MyTest(2)?? 2);
             tm.put(new MyTest(10)?? 10);
             tm.put(new MyTest(10)?? 20);
             for (Entry<MyTest??Integer> e : tm.entrySet())
                 System.out.println("Key = " + e.getKey().key + "?? Value = " + e.getValue());
         }
     }  
     //compareTo of MyTest is called here.
 ???? //compareTo of MyTest is called here.
 ???? //compareTo of MyTest is called here.
 ???? //compareTo of MyTest is called here.
 ???? //Key = 2?? Value = 2
 ???? //Key = 5?? Value = 5
 ???? //Key = 10?? Value = 20

?????????????????????MyTest????????????????????????????compareTo????????ε?????????????????????compareTo?????е??????????????????????????????TreeMap??C++?е?map???????????????????????????????????????????м?????????????????????????Java?У???compareTo????????0????????????????????????????????п???????????????JDK????????????equals????????б?????????????????????????????????????????compareTo??????????????????棬C++?е?map????????????????????????????????????治??????????????????????????????????????

????2??????????????????TreeMap?????????Comparator<T>???????????????????compare??????????TreeMap?????????????????????е???????????′???

public class MyTest {
         private int key;
         public MyTest(int key) {
             this.key = key;
         }
         public static void main(String[] args) {
             TreeMap<MyTest??Integer> tm = new TreeMap<MyTest??Integer>(new Comparator<MyTest>() {
                 @Override
                 public int compare(MyTest first??MyTest second) {
                     System.out.println("compare of Comparator is called here.");
                     return first.key - second.key;
                 }
             });
             tm.put(new MyTest(5)?? 5);
             tm.put(new MyTest(2)?? 2);
             tm.put(new MyTest(10)?? 10);
             for (Entry<MyTest??Integer> e : tm.entrySet())
                 System.out.println("Key = " + e.getKey().key + "?? Value = " + e.getValue());
         }
     }
     //compare of Comparator is called here.
  ????//compare of Comparator is called here.
 ???? //Key = 2?? Value = 2
 ???? //Key = 5?? Value = 5
 ???? //Key = 10?? Value = 10

??????????????????????????Comparator??????????compare????????????TreeMap?????м?????????????

???????????????????C++??map??????????????????????????????map?????????????

????template <class Key?? class Type?? class Traits = less<Key>?? class Allocator=allocator<pair <const Key?? Type> > > class map

???????????????η????????????????????????????????Java??TreeMap????????????????????????map?????????????????????????????????????????????????????????????????????????????????????????????????????

????1????????????????п??????STL???????????Traits?????????????less<Key>?????????????????????????????map?е?????????????????????less<Key>????????

1     template<class _Ty>
2     struct less : public binary_function<_Ty?? _Ty?? bool> {
3         bool operator()(const _Ty& _Left?? const _Ty& _Right) const {
4             return (_Left < _Right);
5         }
6     };