簡體   English   中英

TreeMap具有自己的比較器實現

[英]TreeMap with own comparator implementation

大家。

我編寫了一個TreeMap ,它有自己的方法compare()

目的是按順序對地圖的鍵進行排序:具有較少時間和較少位值的條目應位於頂部。 所有條目都有獨特的時間。 我想在bit: 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;
  }

});

任何人都可以解釋原因嗎?

Ps我用鍵添加了條目:1,2,3,4,5。然后我嘗試用鍵4添加條目,並且沒有添加。 密鑰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;
}   
}

並以這種方式我添加新的東西:

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; bit = false; 時間= 0; 值= aaa ---由於規范大小而放:aaa map [1]

輸入:key = 2; bit = false; 時間= 1; 值= bbb ---因為規范大小而放:bbb map [1,2]

輸入:key = 3; bit = false; 時間= 2; 值= ccc ---因為規范大小而放:ccc map [1,2,3]

輸入:key = 4; bit = false; 時間= 3; Value = ddd --- put with remove map [2,3,4]

輸入:key = 5; bit = false; 時間= 4; 值= www --- put with remove map [3,4,5]

輸入:key = 4; bit = false; 時間= 5; 找到值= rrr!對象[3,4,5]

輸入:key = 6; bit = false; 時間= 6; Value = rrr --- put with remove map [4,5]

輸入:key = 7; bit = false; 時間= 7; 值= rrr ---由於范數大小而放:rrr map [4,5]

輸入:key = 8; bit = false; 時間= 8; 值= rrr ---由於范數大小而放:rrr map [4,5]

輸入:key = 9; bit = false; 時間= 9; 值= rrr ---由於范數大小而放:rrr map [4,5]

TreeMap僅使用比較器來檢查唯一性。
如果根據比較器有2個相等的鍵,則其中一個鍵不會添加到地圖中。 請參閱SortedMap

請注意,如果有序映射要正確實現Map接口,則由有序映射維護的排序(無論是否提供顯式比較器)必須與equals一致。 (有關與equals一致的精確定義,請參閱Comparable接口或Comparator接口。)這是因為Map接口是根據equals操作定義的,但是有序映射使用compareTo(或compare)方法執行所有鍵比較因此,從排序映射的角度來看,這種方法被認為相等的兩個鍵是相等的。 樹圖的行為很明確,即使它的排序與equals不一致; 它只是不遵守Map接口的一般合同。

在您看來,鍵是唯一的(如果您使用equals檢查它們是唯一的),但TreeMap僅使用比較器來檢查唯一性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM