将 ConcurrentHashMap 转换为 Java 中的排序列表

Transform ConcurrentHashMap into a sorted list in Java

在Java8中,计算完ConcurrentHashMap中的数据后,

// this is a working code, which I want to simplify
ConcurrentMap<String, AtomicInteger> data =
    new ConcurrentHashMap<String, AtomicInteger>();
// ... (fill in the data object)
final int l = data.keySet().size();
P[] points = new P[l];
int i = 0;
for (Map.Entry<String, AtomicInteger> entry : data.entrySet())
    points[i++] = new A(entry.getKey(), entry.getValue().get() * 1.0 / 105);
Arrays.sort(points);

我想实现这个:

data.parallelStream()
.map( (k, v) -> new A(k, v.get() * 1.0 / 105) )
.sort().forEach(System.out::println)

如何正确地做到这一点?

我想这就是你想要的。您需要创建一个 StreamEntry 个对象,所以您不能写 (k, v)->.

data.entrySet()
    .parallelStream()
    .map(e -> new A(e.getKey(), e.getValue().get() * 1.0 / 105))
    .sorted()
    .forEachOrdered(System.out::println);