The collection framework in Java (middle)

Source: Internet
Author: User

    • Map and HashMap

 Map interface

    1.MAP provides a mapping relationship where elements are stored as key-value pairs (Key-value) that enable quick lookup of value based on key

  Key-value pairs in 2.Map exist as object instances of type entry

  3. Key (key value) is not repeatable, value can be

  4. Each key can be mapped to at most one value

  The 5.Map interface provides methods for returning a collection of key values, a collection of value values, and a collection of entry (key-value pairs), respectively

  6.MAP supports generics, forms such as:map<k,v>

  HashMap class

  1.HashMap is an important implementation class of map and the most common one, based on hash table implementation

The entry objects in 2.HASHMAP are unordered

Both 3.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)

  

    • Student selection-use map to add students
    • Student Selection-Delete a student from the map
    • Student Selection--Modify the students in the map

PackageCom.imooc.collection;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Scanner;ImportJava.util.Set;PublicClassmaptest {//Used to load student type objectsPublic map<string,student>Students/*Initializing the students property in the constructor*/PublicMaptest () {this.students=New hashmap<string,student>(); }/*** Test add: Enter student ID to determine if it is occupied * If Wei is occupied, enter name, create new Student object, and * add to Student*/PublicvoidTestput () {Scanner console=NewScanner (system.in);int i=0;while (i<3{System.out.println ("Please enter student ID")); String id=Console.next (); Student st=Students.get (ID);if (st==Null) {//Prompt to enter student name System.out.println ("Please enter student name"); String name=Console.next ();//Add a new Student object Student newstudent=NewStudent (Id,name); Students.put (id,newstudent); System.out.println ("Add student successfully:" +Students.get (ID). Name); i++; }Else{System.out.println ("The student ID is occupied");Continue; } } }/***@paramKeyset method for the args test map*/PublicvoidTestkeyset () {//The KeySet method returns the set set of all "keys" in the map set<string> keyset=Students.keyset ();//Get Student capacity System.out.println ("Total:" +students.size () + "Students");//Iterate through the keyset, get each key, and then call the Get method to get the value corresponding to each keyFor(String Stuid:keyset) {Student st=Students.get (STUID);if (st!=Null) {System.out.println ("student" +St.name); } } }/*** Test to delete mappings in map*/PublicvoidTestremove () {//Gets the student ID string to be deleted from the keyboard input Scanner console=NewScanner (system.in);while (True) {//Prompt for student ID System.out.println to be deleted ("Please enter student ID to delete"); String id=Console.next ();//Determine if the ID has a corresponding student object Student st=Students.get (ID);if (st==Null) {//The ID you are prompted for does not exist SYSTEM.OUT.PRINTLN ("The ID does not exist");Continue; } students.remove (ID); System.out.println ("Delete student successfully" +St.name);Break; } }/*** Traverse Map by EntrySet method*/PublicvoidTestentryset () {//With the EntrySet method, all key-value pairs in the map are returned set<entry<string,student>> entryset=Students.entryset ();For (entry<string, student>Entry:entryset) {System.out.println ("Get Key" +Entry.getkey ()); System.out.println ("corresponding job:" +Entry.getvalue (). name); } }/*** Modify existing mappings in map using the Put method*/PublicvoidTestmodify () {//Prompt for the student ID System.out.println to be modified ("Please enter the student ID to be modified:");//Create the Scanner object to get the student ID string entered from the keyboard Scanner console=NewScanner (system.in);WhileTrue){//Gets the student ID entered from the keyboard String stuid=Console.next ();//Find the Student object that corresponds to the student ID from students Student student=Students.get (STUID);if (student==Null{SYSTEM.OUT.PRINTLN ("The ID does not exist, re-enter!") ");Continue; }//Prompt for the current student object's name System.out.println ("Current student ID, the corresponding student is:" +Student.name);//Prompt for a new student name to modify the existing mapping System.out.println ("Please enter a new student name"); String name=Console.next (); Student newstudent=NewStudent (Stuid,name); Students.put (stuid,newstudent); SYSTEM.OUT.PRINTLN ("Modified successfully! "); break;}} /** * @param args *  /public static void main (string[] args) { // TODO Auto -generated method Stub maptest mt=new maptest (); Mt.testput (); Mt.testkeyset (); / *mt.testremove (); Mt.testentryset ();  * /Mt.testmodify (); Mt.testentryset ();}}             

Collection framework in Java (medium)

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.