José Coelho
José Coelho

Reputation: 23

Sort on multiple properties on Integer List

I have a list of integers, and would like to print all their elements in a sorted way, by 2 properties: list size and the sum of the elements on the list. I read on streams thenComparing method, but an error shows up whenever I try to use it.

I wrote some code that is working, but would like to know if there is any other cleaner way to do it. Thanks in advance!

Map<List<Integer>, Double> entropies = new HashMap<>();

// Extra code removed

entropies.keySet()
    .stream().sorted(Comparator.comparingInt(o -> {
        List<Integer> a = (List<Integer>) o;
        return a.size();
    }).thenComparingInt(o -> {
        List<Integer> a = (List<Integer>) o;
        return a.stream().mapToInt(Integer::intValue).sum();
    }))
    .forEach(key -> logger.debug("{} => {}", key, entropies.get(key)));

Same as actual output but without castings would be great.

Upvotes: 2

Views: 56

Answers (1)

Hari Menon
Hari Menon

Reputation: 35415

The following should work:

entropies.keySet()
         .stream().sorted(Comparator.<List<Integer>>comparingInt(List::size)
                              .thenComparingInt(o -> o.stream().mapToInt(Integer::intValue).sum()))
         .forEach(key -> logger.debug("{} => {}", key, entropies.get(key)));

Since Comparator.comparingInt is not able to infer the type as List>. The type hint when you call let's the compiler know the actual type. This is because the sorted method expects Comparator<? super List<Integer>> comparator, so technically your comparator could have any superclass of List, so you need to explicitly specify which superclass it is.

Upvotes: 3

Related Questions