Java Map put key value pair if key is absent
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); for (int i = 0; i < 10; i++) { map.putIfAbsent(i, "Value " + i); }//from ww w. ja v a 2 s .c o m System.out.println(map); } }