How to iterate and modify values in a Map using Java 8
Categories: Java 8(JDK1.8) Java Java Examples
package r4r.co.in;
import java.util.HashMap;
import java.util.Map;
public class IterateAndmodifyValuesInMapUsingJava8 {
public static void main(String[] args) {
// How to iterate and modify values in a Map using Java 8
Map<String, String> map = new HashMap<String, String>();
map.put("1", "1");
map.put("2", "2");
map.put("3", "3");
map.put("4", "4");
map.put("5", "5");
map.put("6", "6");
System.out.println(map);
map.computeIfPresent("1", (key, value) -> "11");
map.computeIfAbsent("7", (value) -> "7");
System.out.println("Modified Map......");
map.entrySet().forEach(System.out::println);
//or
//map.entrySet().iterator().forEachRemaining(System.out::println);
}
}
OutPut:-
{1=1, 2=2, 3=3, 4=4, 5=5, 6=6}
Modified Map......
1=11
2=2
3=3
4=4
5=5
6=6
7=7