2012-04-15 21 views
0

每個人。帶有自己的比較器實現的TreeMap

我寫了一個TreeMap與方法compare()自己的實現。

其目的是按順序對地圖的鍵進行排序:具有較少時間和較少位值的條目應位於頂部。所有參賽作品有獨特的時間我想在位對象之間進行選擇:false - 選擇時間較少的對象。

但下面的Java代碼限制我添加一些新的條目。

private TreeMap<Entry<K>,V> map = new TreeMap<Entry<K>,V>(new Comparator<Entry<K>>() { 

    @Override 
    public int compare(Entry<K> entry1, Entry<K> entry2) { 

    int time1 = entry1.getTime();   
    int time2 = entry2.getTime(); 

    boolean bit1 = entry1.isBit(); 
    boolean bit2 = entry2.isBit(); 

    if (time1 < time2) { 
     if ((bit1 == false && bit2 == true) 
     || (bit1 == false && bit2 == false) 
     || (bit1 == true && bit2 == true)) 
     return -1; 
    } else if (time1 > time2) { 
     if ((bit1 == true && bit2 == false) 
     || (bit1 == true && bit2 == true) 
     || (bit1 == false && bit2 == false)) 
     return 1; 
    } 

    return 0; 
    } 

}); 

任何人都可以請解釋爲什麼嗎?

P.s. 我添加了條目的鍵:1,2,3,4,5。然後我試着用鍵4添加條目,並且它沒有被添加。 Key 1,2 .. - 這意味着我創建3個字段的條目:鍵,位(假 - 默認),時間(計數器創建的唯一值)。 因此,所有參賽選手都是獨一無二的。

這是入門級:

public class Entry<K> { 

private K id; 
private boolean bit; 
private int time; 

public Entry(K id, Boolean bit, int time) { 

    this.setId(id); 
    this.setBit(bit); 
    this.setTime(time); 

} 

public K getId() { 
    return id; 
} 

public void setId(K id) { 
    this.id = id; 
} 

public boolean isBit() { 
    return bit; 
} 

public void setBit(boolean bit) { 
    this.bit = bit; 
} 

public int getTime() { 
    return time; 
} 

public void setTime(int time) { 
    this.time = time; 
} 

public boolean equals(Object o){ 
    if (this.id == ((Entry)o).getId()){ 
     return true; 
    } 
    return false; 
} 
} 

,在這種方式我添加新entrys:

public void put(K key, V value){ 
    entry = new Entry<K>(key, false, clock++); 
    if (map.size() < initialCapacity){ 
     map.put(entry, value); 
    } else { 
     if (this.get(key) == null) { 
      map.remove(map.firstEntry().getKey()); 
      map.put(entry, value); 
     } 
    }   
} 

public V get(K key){ 
    Iterator it = map.keySet().iterator(); 
    while (it.hasNext()){ 
     Entry entry = (Entry) it.next(); 
     if (key.equals(entry.getId())){ 
      entry.setBit(true); 
      return map.get(entry); 
     } 
    }  
    return null; 
} 

運行代碼:

ClockCacheMaximus<BigInteger, Object> ccm = new ClockCacheMaximus<BigInteger, Object>(3);; 
    ccm.put(new BigInteger("1"), "aaa"); 
    System.out.println("map" + ccm.getAll()); 
    System.out.println(); 
    ccm.put(new BigInteger("2"), "bbb"); 
    System.out.println("map" + ccm.getAll()); 
    System.out.println(); 
    ccm.put(new BigInteger("3"), "ccc"); 
    System.out.println("map" + ccm.getAll()); 
    System.out.println(); 
    ccm.put(new BigInteger("4"), "ddd"); 
    System.out.println("map" + ccm.getAll()); 
    System.out.println(); 
    ccm.put(new BigInteger("5"), "www"); 
    System.out.println("map" + ccm.getAll()); 
    System.out.println(); 
    ccm.put(new BigInteger("4"), "rrr"); 
    System.out.println("map" + ccm.getAll()); 
    System.out.println(); 
    ccm.put(new BigInteger("6"), "rrr"); 
    System.out.println("map" + ccm.getAll()); 
    System.out.println(); 
    ccm.put(new BigInteger("7"), "rrr"); 
    System.out.println("map" + ccm.getAll()); 
    System.out.println(); 
    ccm.put(new BigInteger("8"), "rrr"); 
    System.out.println("map" + ccm.getAll()); 
    System.out.println(); 
    ccm.put(new BigInteger("9"), "rrr"); 
    System.out.println("map" + ccm.getAll()); 

結果:

條目:key = 1;位=假;時間= 0;值= aaa ---因規範大小而放置:aaa map [1]

條目:key = 2;位=假;時間= 1;值= BBB ---放因爲規範尺寸:BBB 地圖[1,2]

項:鍵= 3;位=假;時間= 2;值= ccc ---因規範大小而放置:ccc map [1,2,3]

條目:key = 4;位=假;時間= 3;值= ddd ---放入刪除 map [2,3,4]

Entry:key = 5;位=假;時間= 4;值= www ---放入刪除 map [3,4,5]

條目:key = 4;位=假;時間= 5;值= rrr !物體被發現 map [3,4,5]

Entry:key = 6;位=假;時間= 6;值= rrr ---放入刪除 map [4,5]

條目:key = 7;位=假;時間= 7;值= rrr ---由於規範大小:rrr map [4,5]

條目:key = 8;位=假;時間= 8;值= rrr ---由於規範大小:rrr map [4,5]

條目:key = 9;位=假;時間= 9;值= rrr ---由於規範大小:rrr map [4,5]

+0

哪些是你可以添加的條目和​​你不能添加的條目? – 2012-04-15 12:13:58

+0

您需要更清楚地解釋您希望如何訂購'Entry'鍵。舉一些'Entry'對象的例子,以及它們如何出現在你訂購的Map中。 – darrengorman 2012-04-15 12:19:46

+0

問題是你的定義是,如果一個 2012-04-15 12:20:51

回答

2

TreeMap只使用比較器來檢查唯一性。
如果您有兩個按鍵比較相等,則其中一個按鍵不會添加到地圖中。見SortedMap

注意,如果 有序映射要正確實現Map接口,則有序映射(無論是否提供了明確的 比較器)保持的順序必須與equals一致。 (請參閱 Comparable接口或比較器接口,以獲得與equals相同的精確定義 )。這是因爲Map接口是 ,這些接口是根據equals操作定義的,但排序的映射使用其compareTo()函數執行 所有關鍵比較或比較)方法,所以從 的角度看,通過這種方法被認爲相等的兩個密鑰 相等。如果其排序與等號不一致,則樹圖的行爲甚至可以定義爲 ;它只是不服從Map界面的 一般合同。 (如果您有平等檢查,他們是唯一的),但TreeMap只使用比較器來檢查的獨特性

在您看來鑰匙都是獨一無二的。