parrotjack
parrotjack

Reputation: 512

HashMap Sort By Value of AtomicInteger

I have the HashMap and trying to sort reverse order by value, and my below code work ascending order,

LinkedHashMap<String, AtomicInteger> collect = counterMap.entrySet()
                .stream()
                .sorted(Comparator.comparingInt(a -> a.getValue().get()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

Unfortunately i couldn't use reverseOrder() in sorted method.

Any suggestion or idea's are welcome. thanks!

Upvotes: 2

Views: 596

Answers (1)

Andrew
Andrew

Reputation: 49626

You need to help the compiler to resolve the generic types.

Comparator.<Map.Entry<String, AtomicInteger>>comparingInt(a -> a.getValue().get())
          .reversed()

Otherwise, it assumes that a is an Object and a.getValue() will not compile.

This answer of mine I gave a few days ago is closely related.

Upvotes: 4

Related Questions