Map Interface: ============== -> In Java, Map interface is present in the "java.util" package. -> The map interface is different than "collection interface". -> The map interface can use to represent a mapping between a key and value. Ex: bank : icici Features: ========= 1) In map interface, "keys" must be unique. {key1:val1,key2:val2,key1:val3} ==> {key1:val3,key2:val2} 2) In map interface, "values" cannot be unique. {k1:v1,k2:v2,k3:v3,k4:v1,k5:v2} 3) map interface can handle the null values. 4) We can make the map as thread safe using the method "synchronizedMap()". Implementation of Map Interface: ================================ 1) The map interface can be implemented by using: 1) Map interface 2) SortedMap Interface Here: the SortedMap Interface is the sub of Map Interface. 2) using three primary classes: 1) HashMap class 2) TreeMap class 3) LinkedHashMap class Syntax: Map map-object-name = new HashMap<>(); SortedMap map-object-name = new TreeMap<>(); Creation of Map object using Map Interface: =========================================== -> HashMap class and LinkedHashMap class were implemented from "Map Interface". So, when we want to create a map object using map interface, we can use either "HashMap class "or "LinkedHashMap class" only. package pack.p1; import java.util.HashMap; import java.util.Map; public class MainClass { public static void main(String[] args) { Map map = new HashMap<>(); System.out.println("The Map object = "+map); } } ================================================= Creation of Map object using SortedMap interface: ================================================= -> SortedMap interface is the sub of Map interface. -> TreeMap is the class which implements from SortedMap Interface. -> to create a map object using SortedMap interface, we can use "TreeMap class". package pack.p1; import java.util.SortedMap; import java.util.TreeMap; public class MainClass { public static void main(String[] args) { SortedMap map = new TreeMap<>(); System.out.println("The Map object = "+map); } } =============================================== Hierarchy of Map Interface: =========================== Map Interface <=============== SortedMap interface extends Syntax: SortedMap obj extends Map{ } SortedMap <================== TreeMap class implements Syntax: TreeMap obj implements SortedMap{ } Map interface <======================= HashMap implements Map interface <======================= LinkedHashMap implements Syntax: LinkedHashMap obj implements Map{ } ================================================== Why and when we need Map? ========================= 1) map of errors and with its descriptions 2) map of zip codes with its cities. 3) map of managers with employee 4) map of students with class teacher. etc. Operations of Map interface: ============================ -> the SortedMap object is performing the natural sort by default based on the values of map object (but not based on the keys). package pack.p1; import java.util.SortedMap; import java.util.TreeMap; public class MainClass { public static void main(String[] args) { SortedMap map = new TreeMap<>(); System.out.println("The Map object = "+map); // adding of elements into map map.put("apple",100); map.put("banana", 121); map.put("cherry", 198); map.put("Papaya", 67); map.put("mango", 300); System.out.println("The Map after the insertion of elements = "+map); } } ================================================= Iteration over the Map object: =============================== using for Each loop: ===================== package pack.p1; import java.util.Map; import java.util.TreeMap; public class MainClass { public static void main(String[] args) { Map map = new TreeMap<>(); // System.out.println("The Map object = "+map); // adding of elements into map map.put("apple",100); map.put("banana", 121); map.put("cherry", 198); map.put("Papaya", 67); map.put("mango", 300); System.out.println("The Map after the insertion of elements = "+map); for(Map.Entry entry:map.entrySet()) { System.out.println(entry.getKey()+"-->"+entry.getValue()); } // remove operation map.remove("Papaya"); System.out.println("The Map Object after the remove element = "+map); map.clear(); System.out.println("The Map object after the clear operation = "+map); } } =============================================== Can we modify the map object: ============================= package pack.p1; import java.util.Map; import java.util.TreeMap; public class MainClass { public static void main(String[] args) { Map map = new TreeMap<>(); // System.out.println("The Map object = "+map); // adding of elements into map map.put("apple",100); map.put("banana", 121); map.put("cherry", 198); map.put("Papaya", 67); map.put("mango", 300); System.out.println("The Map after the insertion of elements = "+map); // modification map.put("mango", 1000); map.put("papaya", 54); System.out.println("The Map after the modification = "+map); } } ========================================================================== package pack.p1; import java.util.HashMap; import java.util.Map; public class ShoppingCart { public static void main(String[] args) { Map cart = new HashMap<>(); // adding elements cart.put(101, new Product("Laptop",1,75000.0)); cart.put(102, new Product("Smartphone",2,25000.00)); cart.put(103, new Product("Charger",1,2500.00)); cart.put(104, new Product("Earbuds",2,2000.00)); System.out.println("The Shopping cart = "); for(Map.Entry entry : cart.entrySet()) { int productId = entry.getKey(); Product productName = entry.getValue(); System.out.println("Product Id = "+productId+","+productName); } // removing of elements from the cart cart.remove(102); System.out.println("The Shopping cart = "); for(Map.Entry entry : cart.entrySet()) { int productId = entry.getKey(); Product productName = entry.getValue(); System.out.println("Product Id = "+productId+","+productName); } // update Product laptop = cart.get(101); laptop.setQuantity(10); System.out.println("The Shopping cart = "); for(Map.Entry entry : cart.entrySet()) { int productId = entry.getKey(); Product productName = entry.getValue(); System.out.println("Product Id = "+productId+","+productName); } } } class Product{ private String name; private int quantity; private double price; public Product(String name, int quantity, double price) { super(); this.name = name; this.quantity = quantity; this.price = price; } public void setQuantity(int quantity) { this.quantity = quantity; } @Override public String toString() { return "Product [name=" + name + ", quantity=" + quantity + ", price=" + price + "]"; } } ==================================================================== Assignment: =========== Q: DO WE ABLE TO MAKE THE MAP OBJECT AS IMMUTABLE, IF YES HOW? Q: HOW WE CAN USE THE ITERATOR OBJECT FOR MAP OBJECT TO DO THE ITERATION? =============================================================================== ArrayList a = new ArrayList<>(); a.add(10); a.add(20); a.add(30); for(Integer i:a) { Sop(i); }