PackageCom.jihe; Public classEMP {PrivateString e_id; PrivateString E_name; PublicEmp (String e_id, String e_name) {Super(); This. e_id =e_id; This. E_name =E_name; } PublicString gete_id () {returne_id; } Public voidsete_id (String e_id) { This. e_id =e_id; } PublicString Gete_name () {returnE_name; } Public voidsete_name (String e_name) { This. E_name =E_name; } }
PackageCom.jihe;ImportJava.util.*; Public classEmpmap { Public Static voidMain (string[] args) {Map<String,String> map=NewHashMap (); EMP EMP=NewEMP ("001", "Zhang San"); EMP EMP1=NewEMP ("005", "John Doe"); EMP EMP2=NewEMP ("003", "Harry"); EMP Emp3=NewEMP ("008", "Zhao Liu"); Map.put (emp.gete_id (), Emp.gete_name ()); Map.put (emp1.gete_id (), Emp1.gete_name ()); Map.put (emp2.gete_id (), Emp2.gete_name ()); Map.put (emp3.gete_id (), Emp3.gete_name ()); Set<String> keys=Map.keyset (); System.out.println ("Traverse Collection Map:"); Iterator<String> it=Keys.iterator (); while(It.hasnext ()) {String key=It.next (); SYSTEM.OUT.PRINTLN (Key+" "+Map.get (key)); } System.out.println ("Removed is" +map.remove ("005")); Map<string,string>treemap=NewTreeMap (); Treemap.putall (map); Set Set=Treemap.keyset (); System.out.println ("Traversing collection TreeMap:"); Iterator<String> ss=Set.iterator (); while(Ss.hasnext ()) {String str=Ss.next (); System.out.println (str+" "+treemap.get (str)); } }}
Java Collection Exercise: Create a Map collection, create an EMP object, and add the created EMP object to the collection