对于实现了comparable接口的对象,priority queue 具有自动排序的功能,排序依照compareTo(Obj o)方法进行。(还有其他排序方法,不在这里讨论)
对于List,如果其中的对象实现了comparable接口,可以使用 Collections.sort(listName)方法实现,排序依照compareTo(Obj o)方法进行。(还有其他排序方法,不在这里讨论)
TreeMap存储数据时,依照Key类中的compareTo()方法放置映射对(测试表明,如果新加入的key和原有的某个key在compareTo方法看来想等,新的映射对会覆盖掉原有的映射对)。
HashMap存储数据时,依照Key类中的hashCode()方法放置映射对。新旧两个Key,如果hashCode返回值相等,新的覆盖掉旧的;如果hashCode不等,但equals返回值相等,新的依旧会覆盖掉旧的。
下面是实测代码:
Value类:
public class Value implements Comparable<Value> {
private int value = 0;
Value(int value){
this.value = value;
}
@Override
public int compareTo(Value another) {
// TODO Auto-generated method stub
return another.value - this.value;
}
public void print () {
System.out.println("value: " + value);
}
}
public class Key implements Comparable<Key> {
private int key = 0;
Key(int key){
this.key = key;
}
@Override
public int compareTo(Key another) {
return another.key - this.key;
}
@Override
public int hashCode() {
int r = 10000 - key;
return r;
}
// @Override
// //rewrite equals() method based on comparing memory address
// public boolean equals (Object obj){
// if(obj instanceof Key){
// //Compare memory addresses of this and obj
// if (((Key)obj) == this) {
// return true;
// }
// }
// return false;
// }
}
import java.util.*;
public class Test {
public static void main(String[] args) {
Value v0 = new Value(0);
Value v1 = new Value(1);
Value v2 = new Value(2);
Value v3 = new Value(3);
Value v4 = new Value(4);
Key k0 = new Key(0);
Key k1 = new Key(1);
Key k2 = new Key(2);
Key k3 = new Key(3);
Key k4 = new Key(3);//故意设定k3和k4的值相等,这样compareTo方法无法分辨两者
//Test how compareTo works on priority queue
System.out.println("Test priority queue:");
PriorityQueue<Value> pq = new PriorityQueue<Value>();
pq.add(v1);
pq.add(v2);
pq.add(v0);
pq.add(v3);
pq.add(v4);
while (!pq.isEmpty()) {
pq.poll().print();
}
//Test how compareTo works on list
System.out.println("\r\nTest array list:");
List<Value> l = new ArrayList<Value>();
l.add(v1);
l.add(v2);
l.add(v0);
l.add(v3);
l.add(v4);
Collections.sort(l);
while (!l.isEmpty()) {
l.get(0).print();
l.remove(0);
}
//Test how compareTo works on tree map
System.out.println("\r\nTest tree map:");
Map<Key, Value> treeMap = new TreeMap<Key, Value>();
treeMap.put(k1,v1);
treeMap.put(k2,v2);
treeMap.put(k0,v0);
treeMap.put(k3,v3);
treeMap.put(k4,v4);
Collection<Value> c = new ArrayList<Value>();
c = treeMap.values();
Iterator<Value> it = c.iterator();
while(it.hasNext()){
it.next().print();
}
//Test how compareTo works on hash map
System.out.println("\r\nTest hash map:");
//c.clear();
//Collection<Node> c = new ArrayList<Node>();
Map<Key, Value> hashMap = new HashMap<Key, Value>();
hashMap.put(k1,v1);
hashMap.put(k2,v2);
hashMap.put(k0,v0);
hashMap.put(k3,v3);
hashMap.put(k4,v4);
c = hashMap.values();
it = c.iterator();
while(it.hasNext()){
it.next().print();
}
}
}
测试结果如下:
Test priority queue:
value: 4
value: 3
value: 2
value: 1
value: 0
Test array list:
value: 4
value: 3
value: 2
value: 1
value: 0
Test tree map:
value: 4
value: 2
value: 1
value: 0
Test hash map:
value: 4
value: 3
value: 2
value: 1
value: 0