简体   繁体   English

TreeMap具有自己的比较器实现

[英]TreeMap with own comparator implementation

Everyone. 大家。

I write a TreeMap with an own implementation of method compare() . 我编写了一个TreeMap ,它有自己的方法compare()

The aim is to sort the keys of the map in order: entries with less time and less values of bit should be on top. 目的是按顺序对地图的键进行排序:具有较少时间和较少位值的条目应位于顶部。 All entries have unique time. 所有条目都有独特的时间。 I want to choose between objects with bit: false - choose objects with less time. 我想在bit: false之间选择对象 - 选择时间较短的对象。

But the following Java code restricts me to add some new entries. 但是以下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;
  }

});

Can anyone please explain why? 任何人都可以解释原因吗?

Ps I added entry's with keys: 1, 2, 3, 4, 5. Then I tried to add entry with key 4, and It was not added. Ps我用键添加了条目:1,2,3,4,5。然后我尝试用键4添加条目,并且没有添加。 Key 1, 2 .. - it means i create entry with 3 fields: key, bit(false - default), time(unique value created by counter). 密钥1,2 .. - 这意味着我创建具有3个字段的条目:密钥,位(假 - 默认),时间(由计数器创建的唯一值)。 So all entry's im my opninion were unique. 因此,我所有的参赛作品都是独一无二的。

This is entry class: 这是入门级:

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;
}   
}

And in such way I add new 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;
}

Running code: 运行代码:

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());

Result: 结果:

Entry: key = 1; 输入:key = 1; bit = false; bit = false; time = 0; 时间= 0; Value = aaa ---put because of norm size: aaa map[1] 值= aaa ---由于规范大小而放:aaa map [1]

Entry: key = 2; 输入:key = 2; bit = false; bit = false; time = 1; 时间= 1; Value = bbb ---put because of norm size: bbb map[1, 2] 值= bbb ---因为规范大小而放:bbb map [1,2]

Entry: key = 3; 输入:key = 3; bit = false; bit = false; time = 2; 时间= 2; Value = ccc ---put because of norm size: ccc map[1, 2, 3] 值= ccc ---因为规范大小而放:ccc map [1,2,3]

Entry: key = 4; 输入:key = 4; bit = false; bit = false; time = 3; 时间= 3; Value = ddd ---put with remove map[2, 3, 4] Value = ddd --- put with remove map [2,3,4]

Entry: key = 5; 输入:key = 5; bit = false; bit = false; time = 4; 时间= 4; Value = www ---put with remove map[3, 4, 5] 值= www --- put with remove map [3,4,5]

Entry: key = 4; 输入:key = 4; bit = false; bit = false; time = 5; 时间= 5; Value = rrr !object was found map[3, 4, 5] 找到值= rrr!对象[3,4,5]

Entry: key = 6; 输入:key = 6; bit = false; bit = false; time = 6; 时间= 6; Value = rrr ---put with remove map[4, 5] Value = rrr --- put with remove map [4,5]

Entry: key = 7; 输入:key = 7; bit = false; bit = false; time = 7; 时间= 7; Value = rrr ---put because of norm size: rrr map[4, 5] 值= rrr ---由于范数大小而放:rrr map [4,5]

Entry: key = 8; 输入:key = 8; bit = false; bit = false; time = 8; 时间= 8; Value = rrr ---put because of norm size: rrr map[4, 5] 值= rrr ---由于范数大小而放:rrr map [4,5]

Entry: key = 9; 输入:key = 9; bit = false; bit = false; time = 9; 时间= 9; Value = rrr ---put because of norm size: rrr map[4, 5] 值= rrr ---由于范数大小而放:rrr map [4,5]

TreeMap uses only comparator to check uniqueness. TreeMap仅使用比较器来检查唯一性。
If you have 2 keys that are equal according to your comparator then one of them will not be added to map. 如果根据比较器有2个相等的键,则其中一个键不会添加到地图中。 See SortedMap : 请参阅SortedMap

Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if the sorted map is to correctly implement the Map interface. 请注意,如果有序映射要正确实现Map接口,则由有序映射维护的排序(无论是否提供显式比较器)必须与equals一致。 (See the Comparable interface or Comparator interface for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a sorted map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. (有关与equals一致的精确定义,请参阅Comparable接口或Comparator接口。)这是因为Map接口是根据equals操作定义的,但是有序映射使用compareTo(或compare)方法执行所有键比较因此,从排序映射的角度来看,这种方法被认为相等的两个键是相等的。 The behavior of a tree map is well-defined even if its ordering is inconsistent with equals; 树图的行为很明确,即使它的排序与equals不一致; it just fails to obey the general contract of the Map interface. 它只是不遵守Map接口的一般合同。

In your opinion keys are unique (and they unique if you check with equals) but TreeMap uses only comparator to check uniqueness. 在您看来,键是唯一的(如果您使用equals检查它们是唯一的),但TreeMap仅使用比较器来检查唯一性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM