???Java????C++???????????
???????????? ???????[ 2013/4/17 9:53:25 ] ????????
????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 };
??????
![](/images/ad-banner/ad-banner.png)
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11