HashMap<String, Double> missions = new HashMap<>();
missions.put("name", 1.0);
missions.put("name1", 2.0);
missions.keySet().stream().forEach(el-> System.out.println(el));
This prints only the keys, how do I print the map's values instead?
HashMap<String, Double> missions = new HashMap<>();
missions.put("name", 1.0);
missions.put("name1", 2.0);
missions.keySet().stream().forEach(el-> System.out.println(el));
This prints only the keys, how do I print the map's values instead?
Use entrySet()
(or values()
if that's what you need) instead of keySet()
:
Map<String, Double> missions = new HashMap<>();
missions.put("name", 1.0);
missions.put("name1", 2.0);
missions.entrySet().stream().forEach(e-> System.out.println(e));
Entry
). –
Semiliquid Map<String, Double> missions = new HashMap<>();
–
Waring keySet()
, values => values()
, both => entrySet()
; doesn't affect the forEach
. But as I said, it's a style thing—just wanted to mention it. –
Sheply HashMap<String, Double> missions = new HashMap<>();
missions.put("name", 1.0);
missions.put("name1", 2.0);
missions.entrySet().forEach(System.out::println);
Output:
name=1.0
name1=2.0
// Stores the values in a list which can later used
missions.values().stream().collect(Collectors.toList());
or
//print it to standard op
missions.values().forEach(val -> System.out.println(val));
© 2022 - 2024 — McMap. All rights reserved.
forEach(System.out::println)
. However, it's just a matter of style. – Sheply