ConcurrenLinkedtHashmap迭代器提供的元素顺序取决于密钥大小?

时间:2015-06-04 21:36:27

标签: java guava

我发现了(iterator?)ConcurrentLinkedHashMap的奇怪行为。 如果key很长,则通过迭代entrySet / keySet获得的元素是奇怪/意外的顺序。如果钥匙短,一切都好。

以下代码:

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

提供了这样的输出:

 = 0=0
 = 1=1
 = 2=2
 = 3=3
 = 4=4
 = 1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=1
 = 4aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=4
 = 2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=2
 = 3aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=3
 = 0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=0

这很奇怪。 如果我让钥匙更长 - 结果是不同的。

这是一个错误吗?我怎样才能得到正确的结果? (第二个&#39;打印&#39;结果应与第一个相同)

1 个答案:

答案 0 :(得分:5)

  

java.util.LinkedHashMap不同,此类提供可预测的迭代顺序。   (Javadoc

文档相当清楚 - 所以没有错误

关于条目在迭代时的顺序,ConcurrentLinkedHashMap提供关键保留的ascendingdescending顺序。这可能并不总是你的目标,但至少对于给定的测试场景,给出下面的代码返回一个有序的输出:

private static void print(ConcurrentLinkedHashMap<String, String> a)
{
    Iterator iterator = a.entrySet().iterator();
    while (iterator.hasNext())
    {
        System.out.println(" = " + iterator.next());
    }
    Iterator<String> iter = a.ascendingKeySet().iterator();
    while (iter.hasNext()) 
    {
        String key = iter.next();
        System.out.println(key + " -> " + a.get(key));
    }
}

输出:

 = 0=0
 = 1=1
 = 2=2
 = 3=3
 = 4=4
0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 4
 = 1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=1
 = 4aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=4
 = 2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=2
 = 3aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=3
 = 0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=0
0aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> 0
1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> 1
2aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> 2
3aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> 3
4aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> 4