.put().get().containsKey().containsValue().getOrDefault().forEach() // to iterate over.keySet() // for all keys.values() // for all values.replace().replaceAll((key,value) -> value+10).putIfAbsent().remove()
myMap.get("key1");//return 1 (class Integer)// Check whether the Key is in the Map or notmyMap.containsKey(varKey);// Check whether the Value is in the Map or notmyMap.containsValue(varValue);
Check if keys exists
Map<String,String> num =newHashMap<>();num.put("one","first");if (num.containsKey("one")) {System.out.println(num.get("one")); // => first}
get Or Default
Map<Integer,String> map =newHashMap<>();map.put(1,"First element");map.get(1); // => First elementmap.get(2); // => nullmap.getOrDefault(2,"Default element"); // => Default element
LinkedHashMap class is Hash table and Linked list implementation of the Map interface, with predictable iteration order. It inherits HashMap class and implements the Map interface.
The important points about Java LinkedHashMap class are: A LinkedHashMap contains values based on the key. It contains only unique elements. It may have one null key and multiple null values. It is same as HashMap instead maintains insertion order.
publicstaticvoidmain(String arg[]){LinkedHashMap<String,String> lhm =newLinkedHashMap<String,String>();lhm.put("Ramesh","Intermediate");lhm.put("Shiva","B-Tech");lhm.put("Santosh","B-Com");lhm.put("Asha","Msc");lhm.put("Raghu","M-Tech");Set set =lhm.entrySet();Iterator i =set.iterator();while (i.hasNext()) {Map.Entry me = (Map.Entry) i.next();System.out.println(me.getKey() +" : "+me.getValue()); }System.out.println("The Key Contains : "+lhm.containsKey("Shiva"));System.out.println("The value to the corresponding to key : "+lhm.get("Asha"));}
Weakhashmap
Weak References : The objects that are referenced only by weak references are garbage collected eagerly; the GC won’t wait until it needs memory in that case.
publicstaticvoidmain(String args[]) {// Create a hash mapTreeMap tm =newTreeMap();// Put elements to the maptm.put("Zara",newDouble(3434.34));tm.put("Mahnaz",newDouble(123.22));tm.put("Ayan",newDouble(1378.00));tm.put("Daisy",newDouble(99.22));tm.put("Qadir",newDouble(-19.08));// Get a set of the entriesSet set =tm.entrySet();// Get an iteratorIterator i =set.iterator();// Display elementswhile(i.hasNext()) {Map.Entry me = (Map.Entry)i.next();System.out.print(me.getKey() +": ");System.out.println(me.getValue()); }System.out.println();// Deposit 1000 into Zara's accountdouble balance = ((Double)tm.get("Zara")).doubleValue();tm.put("Zara",newDouble(balance +1000));System.out.println("Zara's new balance: "+tm.get("Zara"));}
Hashtable
// not that useful
importjava.util.*;publicclassMain {publicstaticvoidmain(String args[]) {// create and populate hash tableHashtable<Integer,String> map =newHashtable<Integer,String>();map.put(101,"C Language");map.put(102,"Domain");map.put(104,"Databases");System.out.println("Values before remove: "+ map);// Remove value for key 102map.remove(102);System.out.println("Values after remove: "+ map); }}