Map interface
- Map provides a mapping relationship in which elements are stored as key-value pairs (Key-value) that enable quick lookup of value based on key;
- The key values in map exist as object instances of type entry;
- Built (key value) is not repeatable, value values can be repeated, a value can be associated with many key values, each build can only be mapped to one value.
- MAP supports generics, forms such as:map<k,v>
- Use the put (K key,v value) method in map to add
HashMap class
- HashMap is an important implementation class of map and the most common one, based on hash table implementation
- The entry objects in the HashMap are unordered
- Both the key and value values can be null, but a hashmap can have only one mapping with a key value of NULL (the key value is not repeatable)
Let's demonstrate the use of map with an example of adding student objects.
First, create a student class
1 ImportJava.util.HashSet;2 ImportJava.util.Set;3 4 /**5 * Student Class6 * @authorLenovo7 *8 */9 Public classStudent {Ten One PublicString ID; A - PublicString name; - the PublicSet<kecheng>Kecheng; - - PublicStudent (String id,string name) { - This. ID =ID; + This. Name =name; - This. Kecheng =NewHashset<kecheng>(); + } A at}
Then create a Maptest test class, demonstrate how map is used, and create a member method that demonstrates the put () method and the keyset () method
1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 ImportJava.util.Scanner;4 ImportJava.util.Set;5 6 Public classMaptest {7 8 //Create a Map property to load student objects9 PublicMap<string,student>student;Ten One /* A * Initialize student properties in the constructor - */ - Publicmaptest () { the This. Student =NewHashmap<string,student>(); - } - - /* + * Add Method: Enter student ID, determine whether it is occupied, - * If not occupied, enter name, create new Student object, add to student + */ A Public voidTestput () { at //Create a Scanner object, enter student ID -Scanner sc =NewScanner (system.in); - inti = 0; - while(i<3){ -System.out.println ("Please enter student ID:"); -String Stuid =Sc.next (); inStudent stu =Student.get (stuid); - if(Stu = =NULL){ toSystem.out.println ("Please enter student's name:"); +String Stuname =Sc.next (); -Student newstudent =NewStudent (stuid,stuname); the student.put (Stuid, newstudent); *System.out.println ("Add student successfully:" +Student.get (stuid). name); $i++;Panax Notoginseng}Else{ -System.out.println ("The student ID has been occupied!") "); the Continue; + } A the } + } - $ /* $ * Keyset method for testing map - */ - Public voidTestkeyset () { the //returns the set set of all "keys" in the map by using the Keyset method -Set<string> KeySet =Student.keyset ();Wuyi //get the student capacity theSystem.out.println ("A total of" +student.size () + "students;"); - //iterate through the keyset, get each key, and then call the Get method to get the value corresponding to each key Wu for(String stuid:keyset) { -Student stu =Student.get (stuid); About if(Stu! =NULL){ $System.out.println ("Student:" +stu.name); - } - } - } A + Public Static voidMain (string[] args) { the -maptest MT =Newmaptest (); $ mt.testput (); the Mt.testkeyset (); the } the the}
The result after running the main method is as follows:
Please enter student ID: 1 Please enter student's name:Tom successfully added student: Tom Please enter student ID: 2 Please enter student's name:Jack successfully added student: Jack Please enter student ID: 3 Please enter student name:Lily successfully added students: Lily has a total of 3 students; Student: Tom student: Jack Student: Lily
Remove mappings in map using the Remove () method in map
1 /*2 * Delete mappings in map3 */4 Public voidTestremove () {5Scanner sc =NewScanner (system.in);6 while(true){7System.out.println ("Please enter the student ID to delete:");8String Stuid =Sc.next ();9 //determine if the input ID has a corresponding student objectTenStudent stu =Student.get (stuid); One if(Stu = =NULL){ ASystem.out.println ("The student ID entered does not exist!") "); - Continue; - } the Student.remove (stuid); -System.out.println ("Delete student successfully" +stu.name); - Break; - } + Testentryset (); -}
Use the EntrySet () method to traverse a map
1 /*2 * Traverse map via EntrySet.3 */4 Public voidTestentryset () {5 //returns all the key-value pairs in the map via EntrySet6Set<entry<string,student>> EntrySet =Student.entryset ();7 for(entry<string,student>Entry:entryset) {8SYSTEM.OUT.PRINTLN ("Get key:" +Entry.getkey ());9System.out.println ("The corresponding value is:" +Entry.getvalue (). name);Ten } One}
Use the put () method to modify a map that already exists in the map
1 /*2 * Use the Put method to modify existing mappings in the map3 */4 Public voidtestmodify () {5System.out.println ("Please enter the student ID to be modified:");6Scanner sc =NewScanner (system.in);7 while(true){8String ID =Sc.next ();9Student stu =student.get (ID);Ten if(Stu = =NULL){ OneSYSTEM.OUT.PRINTLN ("ID does not exist! "); A Continue; - } -System.out.println ("Current student is:" +stu.name); theSystem.out.println ("Please enter a new student:"); -String name =Sc.next (); -Student Newstu =NewStudent (id,name); - student.put (ID, newstu); +SYSTEM.OUT.PRINTLN ("Modified successfully! "); - Break; + } A}
How to use the map interface in the Java collection