みんな。
TreeMap
method の独自の実装でを作成し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;
}
});
誰でも理由を説明できますか?
Ps キー 1、2、3、4、5 でエントリを追加しました。次に、キー 4 でエントリを追加しようとしましたが、追加されませんでした。キー 1、2 .. - キー、ビット (false - デフォルト)、時間 (カウンターによって作成された一意の値) の 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());
結果:
エントリ: キー = 1; ビット = 偽; 時間 = 0; 値 = aaa --- ノルム サイズ: aaa map[1] のために配置
エントリ: キー = 2; ビット = 偽; 時間 = 1; 値 = bbb --- ノルム サイズのため配置: bbb map[1, 2]
エントリ: キー = 3; ビット = 偽; 時間 = 2; 値 = ccc --- ノルム サイズの理由: ccc map[1, 2, 3]
エントリ: キー = 4; ビット = 偽; 時間 = 3; 値 = ddd ---マップを削除して配置[2、3、4]
エントリ: キー = 5; ビット = 偽; 時間 = 4; 値 = www ---マップを削除して配置[3、4、5]
エントリ: キー = 4; ビット = 偽; 時間 = 5; 値 = rrr ! オブジェクトが見つかりました map[3, 4, 5]
エントリ: キー = 6; ビット = 偽; 時間 = 6; 値 = rrr ---マップを削除して配置[4、5]
エントリ: キー = 7; ビット = 偽; 時間 = 7; 値 = rrr --- ノルム サイズのために配置: rrr マップ[4, 5]
エントリ: キー = 8; ビット = 偽; 時間 = 8; 値 = rrr --- ノルム サイズのために配置: rrr マップ[4, 5]
エントリ: キー = 9; ビット = 偽; 時間 = 9; 値 = rrr --- ノルム サイズのために配置: rrr マップ[4, 5]