List of usage examples for java.util HashMap HashMap
public HashMap()
From source file:Main.java
public static void main(String[] a) { Map<String, String> map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); System.out.println(map.get("key2")); System.out.println(map.containsKey("key2")); System.out.println(map.containsKey("key4")); }
From source file:Main.java
public static void main(String args[]) { HashMap<Integer, String> newmap = new HashMap<Integer, String>(); // populate hash map newmap.put(1, "tutorials"); newmap.put(2, "from"); newmap.put(3, "java2s.com"); System.out.println("Map value before change: " + newmap); // put new values at key 3 String prevvalue = (String) newmap.put(3, "is great"); // check returned previous value System.out.println("Returned previous value: " + prevvalue); System.out.println("Map value after change: " + newmap); }
From source file:Main.java
public static void main(String args[]) { HashMap<Integer, String> newmap2 = new HashMap<Integer, String>(); HashMap<Integer, String> newmap1 = new HashMap<Integer, String>(); // populate hash map newmap1.put(1, "tutorials"); newmap1.put(2, "from"); newmap1.put(3, "java2s.com"); System.out.println("Values in newmap1: " + newmap1); // put all values in newmap2 newmap2.putAll(newmap1);/*from w ww. j a v a 2 s .c om*/ System.out.println("Values in newmap2: " + newmap2); }
From source file:Main.java
public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); for (int i = 0; i < 10; i++) { map.putIfAbsent(i, "val" + i); }/* w ww. jav a 2 s . c o m*/ map.forEach((id, val) -> System.out.println(val)); }
From source file:Main.java
public static void main(String... args) { Map<String, MyData> map = new HashMap<String, MyData>(); map.put("A", new MyData()); System.out.println(map);//www . j a va2s . c om }
From source file:Main.java
public static void main(String[] a) { HashMap<String, String> map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); map.remove("key3"); System.out.println(map);// w w w .j a v a 2s.c om }
From source file:Main.java
public static final void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("one", "a"); System.out.println("Size = " + map.size()); map.put(null, "b"); System.out.println("Size = " + map.size()); System.out.println("map.get(null) = " + map.get(null)); }
From source file:Main.java
public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("CSS", "style"); map.put("HTML", "mark up"); map.put("Oracle", "database"); map.put("XML", "data"); map.forEach((String key, String value) -> { System.out.println("key=" + key + ", value=" + value); });/*ww w . ja va 2 s. co m*/ }
From source file:Main.java
public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); for (int i = 0; i < 10; i++) { map.putIfAbsent(i, "val" + i); }/*from w ww . j a v a 2 s. com*/ map.forEach((id, val) -> System.out.println(val)); System.out.println(map.getOrDefault(42, "not found")); // not found }
From source file:Main.java
public static void main(String[] a) { Map<String, String> map = new HashMap<String, String>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); map.put(null, null);//from w w w . ja v a 2s . c o m Map<String, String> map2 = new HashMap<String, String>(); map2.put("key4", "value4"); map2.put("key5", "value5"); map2.put("key6", "value6"); map.putAll(map2); System.out.println(map); }