Packagecom.test;ImportJava.util.*; Public classDemo7_3 { Public Static voidMain (string[] args) {//TODO auto-generated Method StubEmp emp1=NewEMP ("s001", "AAA", 12.3f); EMP EMP2=NewEMP ("s002", "BBB", 5.3f); EMP Emp3=NewEMP ("s003", "Li Kui", 52f); //Create a HashMap objectHashMap HM =NewHashMap (); //put the EMP into HMHm.put ("s001", EMP1); Hm.put ("S002", EMP2); //overrides the previous value and does not allow K to repeat//hm.put ("s002", Emp3);Hm.put ("S003", Emp3); //If you're looking for people with numbers that are s002, if(Hm.containskey ("s002"))//returns a Boolean value{System.out.println ("There is the staff"); //How to remove, keys <--> valuesEMP emp = (EMP) hm.get ("s002"); System.out.println ("Employee name is" +emp.getname ()); } Else{System.out.println ("No Staff"); } //traverse all keys and value in the HashMap//iteratorsIterator it =Hm.keyset (). iterator (); //Hasnext Returns a Boolean value to see if there is a next//The HashMap are out of order, not taken out in advance. while(It.hasnext ()) {//Remove key It.next () returns an object type, converted to a string using ToString//depending on the data type of the key value you wrote before, define a variable of the corresponding data type to receiveString key =It.next (). toString (); EMP EMP=(EMP) hm.get (key); System.out.println ("Name is" +emp.getname ()); System.out.println ("Salary is" +emp.getsal ()); } }}//Employee Management ClassclassEmpmanage {PrivateArrayList Al =NULL; //The main function of constructors is to initialize member variables PublicEmpmanage () {Al=NewArrayList (); } //Join the Staff Public voidaddemp (emp emp) {al.add (EMP); } //display information about the employee according to the employee number Public voidshowinfo (String empNo) {//traverse the entire ArrayList for(inti = 0; I < al.size (); i++) { //Remove EMP ObjectEMP emp =(EMP) al.get (i); //Compare Numbers if(Emp.getempno (). Equals (EmpNo)) {System.out.println ("Find the employee, his message is"); System.out.println ("Number =" +empNo); System.out.println ("First name =" +emp.getname ()); System.out.println ("Wage =" +emp.getsal ()); } } } //Revise salary Public voidUpdatesal (String EmpNo,floatnewsal) { for(inti = 0; I < al.size (); i++) {EMP emp=(EMP) al.get (i); if(Emp.getempno (). Equals (EmpNo)) {//Revise salaryemp.setsal (newsal); } } } //Delete Employee Public voiddelemp (String empNo) { for(inti = 0; I < al.size (); i++) {EMP emp=(EMP) al.get (i); if(Emp.getempno (). Equals (EmpNo)) {//two ways to deleteAl.remove (i); //al.remove (EMP); } } }}//Employee ClassclassEMP {//School Number PrivateString EmpNo; PrivateString name; Private floatSal; //constructor Function PublicEMP (String empNo, string name,floatSal) { Super(); This. EmpNo =EmpNo; This. Name =name; This. Sal =Sal; } PublicString Getempno () {returnEmpNo; } Public voidsetempno (String empNo) { This. EmpNo =EmpNo; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public floatgetsal () {returnSal; } Public voidSetsal (floatSal) { This. Sal =Sal; }}
HashMap of the Java Collection class