I'm new to Java and is trying to learn the concept of Maps.
I have came up with the code below. However, I want to print out the "key String" and "value String" at the same time.
ProcessBuilder pb1 = new ProcessBuilder();
Map<String, String> mss1 = pb1.environment();
System.out.println(mss1.size());
for (String key: mss1.keySet()){
System.out.println(key);
}
I could only find method that print only the "key String".
This question is related to
java
dictionary
key
final Map<String, String> mss1 = new ProcessBuilder().environment();
mss1.entrySet()
.stream()
//depending on how you want to join K and V use different delimiter
.map(entry ->
String.join(":", entry.getKey(),entry.getValue()))
.forEach(System.out::println);
Inside of your loop, you have the key, which you can use to retrieve the value from the Map
:
for (String key: mss1.keySet()) {
System.out.println(key + ": " + mss1.get(key));
}
Source: Stackoverflow.com