I found strange behaviour of (iterator?) ConcurrentLinkedHashMap. Elements obtained by iterating entrySet/keySet are in strange/unexpected order if key is long. Everything is ok if key is short.
Following code:
public static void main(String[] args) {
ConcurrentLinkedHashMap<String, String> map =
new ConcurrentLinkedHashMap.Builder<String, String>().maximumWeightedCapacity(1000).build();
for (int i = 0; i < 5; i++) {
map.put(i + "", i + "");
}
print(map);
map.clear();
// NOW AGAIN THE SAME, BUT WITH LONG KEY
for (int i = 0; i < 5; i++) {
map.put(i + "aaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaa" +
"aaaaaaaaaaaaaaaaaaaaaaaaaa", i + "");
}
print(map);
}
private static void print(ConcurrentLinkedHashMap<String, String> a) {
Iterator iterator = a.entrySet().iterator();
while (iterator.hasNext()) {
System.out.println(" = " + iterator.next());
}
}
provides such output:
= 0=0
= 1=1
= 2=2
= 3=3
= 4=4
= 1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=1
= 4aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=4
= 2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=2
= 3aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=3
= 0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=0
That is quite strange. If i make key longer - result is different.
Is it a bug? How could I get correct result? (second 'print' result should be in the same order as first)