Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java8 Stream to find the highest values from map

I wrote following method to find the keys mapped to the highest values and trying to convert to java Streams. Can you please help?

private List<Integer> testStreamMap(Map<Integer, Long> mapGroup) 
{
    List<Integer> listMax = new ArrayList<Integer>();
    Long frequency = 0L;
    for (Integer key : mapGroup.keySet()) {
        Long occurrence = mapGroup.get(key);
        if (occurrence > frequency) {
            listMax.clear();
            listMax.add(key);
            frequency = occurrence;
        } else if (occurrence == frequency) {
            listMax.add(key);
        }
    }
    return listMax;
}
like image 677
Dumy Avatar asked Feb 06 '17 04:02

Dumy


People also ask

How do you find the maximum value of a map using streams?

Integer max=mapGroup. entrySet(). stream(). max(Map.

How will you get the highest number present in a list using Java 8?

2. Using Stream. max() method. The idea is to convert the list into a Stream and call Stream#max() that accepts a Comparator to compare items in the stream against each other to find the maximum element, and returns an Optional containing the maximum element in the stream according to the provided Comparator .

How do you get a specific value from a map?

HashMap get() Method in Java HashMap. get() method of HashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key.

Can we use map Stream in Java 8?

Java 8 Stream's map method is intermediate operation and consumes single element forom input Stream and produces single element to output Stream. It simply used to convert Stream of one type to another. Let's see method signature of Stream's map method.


2 Answers

You can get a single key via

Integer max=mapGroup.entrySet().stream().max(Map.Entry.comparingByValue()).get().getKey();

but unfortunately, there is no built-in function for getting all equivalent maximums.

The simplest, straight-forward solution is to find the maximum value first and retrieve all keys mapping to that value afterwards:

private List<Integer> testStreamMap(Map<Integer, Long> mapGroup) {
    if(mapGroup.isEmpty())
        return Collections.emptyList();
    long max = mapGroup.values().stream().max(Comparator.naturalOrder()).get();
    return mapGroup.entrySet().stream()
        .filter(e -> e.getValue() == max)
        .map(Map.Entry::getKey)
        .collect(Collectors.toList());
}

Solutions for getting all maximum values of a stream in a single pass, are discussed in “How to force max() to return ALL maximum values in a Java Stream?”. You will see that single-pass solutions are much more complicated and not worth the effort if your input is an ordinary Map (e.g. HashMap), which can be iterated multiple times cheaply.

like image 51
Holger Avatar answered Oct 09 '22 16:10

Holger


I'm not sure what half your code is trying to do, but to answer your question as per the title, which I'm guessing was meant to be "find the entry with the highest value":

Map.Entry<Integer, Long> maxEntry = map.entrySet().stream()
  .max(Map.Entry.comparingByValue()).get();
like image 39
Bohemian Avatar answered Oct 09 '22 16:10

Bohemian