-1
Map<String,Integer> hm = new HashMap<>();

hm contains name and Age of Employees. How to find the names of all employees whose age > 25 using java 8 streams concept

I attempted like this

hm.stream().filter(x->Map.Entry.getValue(x)>25).collect(collectors.toList());

Could anyone correct me?

Progman
  • 16,827
  • 6
  • 33
  • 48
medha
  • 1
  • 2
  • "I attempted like this hm.stream().filter(x->Map.Entry.getValue(x)>25).collect(collectors.toList());" - What was the problem with that? – shreyasm-dev Oct 12 '20 at 16:24
  • Do you want to have map's value with v>25 ? so do like `hm.values().stream().filter(x -> x > 25).collect(Collectors.toList());` – Hadi J Oct 12 '20 at 17:18
  • 1
    Age of Employees? Is that a variant of Age of Empires? Cool! – MC Emperor Oct 12 '20 at 19:40

1 Answers1

1

You can't get stream over Map directly. You can get .entrySet() of the map then filter by age and collect names in a list.

List<String> list = hm.entrySet().stream()
                                 .filter(x -> x.getValue() > 25)
                                 .map(e -> e.getKey())
                                 .collect(Collectors.toList());
Eklavya
  • 17,618
  • 4
  • 28
  • 57