Creates a map collection, creates an EMP object, adds the created EMP object to the collection, and removes the object with ID 005 from the collection
To create an EMP class:
Package Jihe;public class Emp {private string Id;public string GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} private string Name;public EMP (string ID, string name) {super (); this.id = Id;this.name = name;}
Create a Map class and test:
Package Jihe;import java.util.*;p ublic class Lianxi1 {public static void main (string[] args) {map<string,string> ma P=new hashmap<> (); EMP emp=new EMP ("001", "Hamburg"); EMP emp1=new EMP ("002", "french fries"); EMP emp2=new EMP ("003", "coffee"); EMP emp3=new EMP ("004", "milk"); EMP emp4=new EMP ("005", "chicken Wings"); EMP emp5=new EMP ("006", "chicken Leg"), Map.put (Emp.getid (), Emp.getname ()), Map.put (Emp1.getid (), Emp1.getname ()), Map.put ( Emp2.getid (), Emp2.getname ()), Map.put (Emp3.getid (), Emp3.getname ()), Map.put (Emp4.getid (), Emp4.getname ()); map.put (Emp5.getid (), Emp5.getname ()); Map.Remove (Emp4.getid ()); Set <String> k=map.keyset ();iterator<string> it=k.iterator (); while (It.hasnext ()) {String str=it.next (); System.out.println (str+ " " +map.get (str));}}}
Test results:
Java Collection Exercise--3