Все.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 и не был добавлен. Ключ 1, 2 .. - это означает, что я создаю запись с тремя полями: ключ, бит (false - по умолчанию), время (уникальное значение, созданное счетчиком). Итак, все записи были уникальными.
Это класс вход:
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());
Результат:
Ввод: ключ = 1; бит = false; time = 0; Значение = aaa --- положить из-за нормы размер: aaa map [1]
Ввод: ключ = 2; бит = false; time = 1; Значение = bbb --- положить из-за нормы размер: bbb map [1, 2]
Ввод: ключ = 3; бит = false; время = 2; Значение = ccc --- положить из-за нормы размер: ccc map [1, 2, 3]
Ввод: ключ = 4; бит = false; время = 3; Значение = ddd --- положить с удалением map [2, 3, 4]
Ввод: ключ = 5; бит = false; время = 4; Значение = www --- положить с удалением map [3, 4, 5]
Ввод: ключ = 4; бит = false; время = 5; Значение = rrr !объект был найден map [3, 4, 5]
Ставка: ключ = 6; бит = false; время = 6; Значение = rrr --- положить с удалением map [4, 5]
Ввод: ключ = 7; бит = false; время = 7; Значение = rrr --- положить из-за нормы размер: rrr map [4, 5]
Ввод: ключ = 8; бит = false; время = 8; Значение = rrr --- положить из-за нормы размер: rrr map [4, 5]
Ввод: ключ = 9; бит = false; время = 9; Значение = rrr --- положить из-за нормы размер: rrr map [4, 5]
которые были записями, которые вы могли добавить, и те, которые вы не могли добавить? –
Вам нужно более четко объяснить, как вы хотите заказать клавиши «Ввод». Приведите пример объектов «Entry» и как они появятся в вашей упорядоченной «карте». – darrengorman
Проблема в том, что ваше определение состоит в том, что если a