The Java Collection Framework (10)

Source: Internet
Author: User

Map and HashMap

Map interface:
Map provides a mapping relationship in which the elements are in the form of key-value pairs (key-value)
stored, able to quickly find value based on key
The key value in map exists as an object instance of type Evtry
Key value (key) is not repeatable and value value can be repeated
Each key can be mapped to at most one value
The map interface provides a way to return a collection of key values, a value collection, and a collection of entry (key-value pairs)
Ma p supports generics, forms such as:map<k,v>

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 UNLL, but a hashmap can have only one
A mapping with a null key value (the key value cannot be duplicated)

Case List Function Description

Student Information management through map<string,student>
Where the key value is the student's id,value for the student's object

Enter student information via keyboard

increase, delete, change, and check the student information in the collection

1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 ImportJava.util.Map.Entry;4 ImportJava.util.Scanner;5 ImportJava.util.Set;6 7  Public classMaptest {8 9     //used to load student type objects//Here the student is the last written student class (refer to the Java Collection Framework (9))Ten      PublicMap<string, student>students; One     //initializing the Students property in the constructor A      Publicmaptest () { -          This. Students =NewHashmap<string, student>(); -     } the      -     /* - * Test add: Enter student ID to determine if it is occupied - * If not occupied, enter a name, create a student object, and + * Added to students -      */ +      Public voidTestput () { A         //Create a Scanner object to get the student ID and name entered atScanner console =NewScanner (system.in); -         inti = 0; -          while(I < 3){ -System.out.println ("Please enter student ID:"); -String ID =Console.next (); -             //determine if ID is occupied inStudent st =Students.get (ID); -             if(St = =NULL){ to                 //prompt to enter student's name +System.out.println ("Please enter student's name:"); -String name =Console.next (); the                 //Create a new student object *Student newstudent =NewStudent (id,name); $                 //add id-Student mappings by calling the put method of studentsPanax Notoginseng students.put (ID, newstudent); -System.out.println ("Add student successfully:" +Students.get (ID). name); thei + +; +             } A             Else{ theSystem.out.println ("The student ID has been occupied!") "); +                 Continue; -             } $         } $     } -      -     //Keyset method for testing map the      Public voidTestkeyset () { -         //returns the set set of all "keys" in the map by using the Keyset methodWuyiSet<string> KeySet =Students.keyset (); the         //get the students capacity -System.out.println ("A total of:" + students.size () + "Students! "); Wu         //iterate through the keyset, get each key, and call the Get method to get the value corresponding to each key -          for(String stuid:keyset) { AboutStudent st =Students.get (stuid); $             if(St! =NULL){ -System.out.println ("Student:" +st.name); -             } -         } A     } +      the     //test to delete existing mappings in map -      Public voidTestremove () { $         //Gets the student ID string to be deleted from the keyboard input theScanner console =NewScanner (system.in); the          while(true){ the             //prompt for the ID of the student you want to delete theSystem.out.println ("Please enter the student to be deleted id! "); -String ID =Console.next (); in             //determine if the ID has a corresponding student object theStudent st =Students.get (ID); the             if(St = =NULL){ About                 //The ID you are prompted for does not exist theSYSTEM.OUT.PRINTLN ("The ID does not exist!") "); the                 Continue; the             } + Students.remove (ID); -System.out.println ("Successfully deleted students:" +st.name); the              Break;Bayi         } the     } the      -     //Traverse Map by EntrySet method -      Public voidTestentryset () { the         //returns all the key-value pairs in the map by using the EntrySet method theSet<entry <String,Student>> EntrySet =Students.entryset (); the          for(Entry<string, student>Entry:entryset) { theSYSTEM.OUT.PRINTLN ("Get key:" +Entry.getkey ()); -SYSTEM.OUT.PRINTLN ("corresponding value:" +Entry.getvalue (). name); the              the         } the     }94      the     //Modifying existing mappings in a map using the Put method the      Public voidtestmodify () { the         //prompt to enter student ID to be modified98System.out.println ("Please enter the student ID to be modified:"); About         //Create a Scanner object to get the student ID string entered from the keyboard -Scanner console =NewScanner (system.in);101          while(true ){102             //Gets the student ID entered from the keyboard103String Stuid =Console.next ();104             //find the Student ID that corresponds to the student object from students theStudent Student =Students.get (stuid);106             if(Student = =NULL){107SYSTEM.OUT.PRINTLN ("The ID does not exist!") Please enter the new! ");108                 Continue;109             } the             //indicates the name of the current student object111System.out.println ("Current student ID, the corresponding student is:" +student.name); the             //prompt for a new student name to modify the existing mapping113System.out.println ("Please enter a new student name:"); theString name =Console.next (); theStudent newstudent =NewStudent (stuid,name); the students.put (Stuid, newstudent);117SYSTEM.OUT.PRINTLN ("Modified successfully! ");118              Break;119         } -     }121     122      Public Static voidMain (string[] args) {123 124maptest MT =Newmaptest (); the mt.testput ();126 Mt.testkeyset ();127 Mt.testremove (); - Mt.testentryset ();129 mt.testmodify (); the Mt.testentryset ();131     } the 133}

The Java Collection Framework (10)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.