Creates a map collection, creates an EMP object, and creates an EMP object in the field collection (the ID of the EMP object as the key to the Map collection), and removes the object with ID 005 from the collection.
PackageCom.hanqi.jihe; Public classEMP {PrivateString e_id; PrivateString E_name; PublicEmp (String e_id,string e_name) { 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.hanqi.jihe;ImportJava.util.*; Public classTESTMAP2 { Public Static voidMain (string[] args) {Map<String,String> map=NewHashmap<>(); EMP e=NewEMP ("001", "one"); EMP E2=NewEMP ("003", "three"); EMP E3=NewEMP ("005", "five"); EMP e4=NewEMP ("007", "seven"); Map.put (e.gete_id (), E.gete_name ()); Map.put (e2.gete_id (), E2.gete_name ()); Map.put (e3.gete_id (), E3.gete_name ()); Map.put (e4.gete_id (), E4.gete_name ()); Map.Remove ("005"); Set Set=Map.keyset (); Iterator<string>it=Set.iterator (); while(It.hasnext ()) {String s=It.next (); String N=Map.get (s); System.out.println (S+" "+N); } }}
Java Collection Exercises 3