1

I'm new to java and I'm tinkering with forEach loops.

I want to use entry.getValue() outside of the loop like this:

hashmap.entrySet().stream()
    .sorted(Map.Entry<String,Double>comparingByValue())
    .limit(1).forEach(entry->
        {System.out.print("Worst output: " + entry.getValue() + ");}
    );
....//print the one iteration of previous loop, or use entry.getValue() as var
8
  • 4
    Just to be certain - you're looking for the lowest value in the map? Commented Feb 11, 2017 at 15:01
  • 3
    So what's the result of what you're already doing? Commented Feb 11, 2017 at 15:01
  • 5
    You shouldn't sort, and should't limit, and shouldn't use forEach to find the minimum value. Instead, you should use min(): docs.oracle.com/javase/8/docs/api/java/util/stream/… Commented Feb 11, 2017 at 15:03
  • 4
    I repeat: you shouldn't have any forEach loop. To find the min() value of a stream, use the min() method. Not sort() followed by limit() followed by forEach. Commented Feb 11, 2017 at 15:11
  • 3
    This looks like X/Y problem. @Heist you can't use variable outside of its declared scope (in this case lambda). So what you are asking is impossible. But you can use its value, for instance by returning it like in case of min() method already mentioned here. Commented Feb 11, 2017 at 15:11

3 Answers 3

3

I believe you want to map your entries to double and then use DoubleStream.min() with something like,

double worst = hashmap.entrySet().stream()
        .mapToDouble(Map.Entry<String,Double>::getValue).min().getAsDouble();
System.out.println("Worst output: " + worst);

If you actually want the maximum value use max() instead of min().

Sign up to request clarification or add additional context in comments.

3 Comments

Can i get the corresponding key to that value, and put it in something like String w = ...
This will throw NoSuchElementException in the case of an empty map. The original code would simply not print anything.
You don’t need to map with Map.Entry<String,Double>::getValue, if you stream over values() rather than entrySet() in the first place. And if we don’t stream, the classical solution is as simple as double worst = Collections.min(hashmap.values());
2

You could simply use the max method to get the highest value:

Double max = hashmap.values()
                    .stream()
                    .max(Comparator.naturalOrder())
                    .orElse(null);

2 Comments

This doesn't compile
@Buhb stupid copy-paste mistake, thanks - edited and fixed.
2

If you want to do something with the entire Map.Entry, and also elegantly handle empty maps, I suggest you do it with Optional.

Optional<Map.Entry<String, Double>> o = hashmap.entrySet().stream().min(Map.Entry.comparingByValue());
o.ifPresent(entry -> System.out.println(entry.getKey() + " " + entry.getValue()));

This way, if there is at least one entry in the map, the lowest one is returned, and since it's wrapped in an optional, you can easily handle the empty case as well. In the sample code above, it will print both the key and the value, if it's present, or do nothing if it isn't.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.