Java-set framework Map, java framework map

Source: Internet
Author: User

Java-set framework Map, java framework map

  • Map Interface

Map provides a ing relationship where elements are stored in the form of key-value pairs. Keys and values can be any type of objects, you can quickly find the value based on the key.

Key-value pairs in Map exist as Entry-type object instances.

Keys (key values) cannot be repeated, and values can.

Each key can only be mapped to one value at most. One value can correspond to multiple keys.

The Map interface provides methods to return the key value set, value set, and Entry (key-value Pair) set. The Entry class is an internal class of Map.

Map supports generics in the form of Map <Key value type, V value type>

  • HashMap class

HashMap is an important implementation class of Map and is also the most commonly used. It is implemented based on hash tables.

The Entry object in HashMap isUnorderedArranged.

Both the Key value and value can be null, but a HashMap can only have a null ing with the key value being null.

MapTest. java

Package com. test. collection; import java. util. hashMap; import java. util. hashSet; import java. util. map; import java. util. map. entry; import java. util. imports; import java. util. set; public class MapTest {public Map <String, Student> students; public MapTest () {this. students = new HashMap <String, Student> () ;}/ *** add */public void testPut () {login console = new Login (System. in); for (int I = 0; I <3; I ++) {System. out. println ("input student ID:"); String studentId = console. next (); // determine whether the ID is occupied Student st = students. get (studentId); if (st = null) {// enter the Student name System. out. println ("input Student name:"); String studentName = console. next (); // create the Student object Student newStudent = new Student (studentId, studentName); // Add the ID-Student ing students by calling the students put method. put (studentId, newStudent); System. out. println ("student added successfully:" + students. get (StudentId ). name);} else {System. out. println ("this student ID is occupied"); continue ;}}/ *** traverse Map */public void testKeySet () using the keySet method {// use the keySet method, returns the Set <String> keySet = students of all "keys" in the Map. keySet (); // obtain the capacity of students System. out. println ("Total" + students. size () + "student"); // traverses the keySet to obtain each key, and then calls the get method to obtain the value for each key (String stuId: keySet) {Student st = students. get (stuId); if (st! = Null) {System. out. println ("Student:" + st. name) ;}}/ *** Delete */public void testRemove () {console = new release (System. in); while (true) {System. out. println ("Enter the student ID to delete:"); String studentId = console. next (); Student st = students. get (studentId); if (st = null) {System. out. println ("this student ID does not exist"); continue;} students. remove (studentId); System. out. println ("successfully deleted STUDENT:" + st. name); break ;}}/*** use the entrySet method to traverse Map */public void testEntrySet () {Set <Entry <String, Student> entrySet = students. entrySet (); System. out. println ("Total" + students. size () + "Student"); for (Entry <String, Student> entry: entrySet) {System. out. println ("key value:" + entry. getKey (); System. out. println ("corresponding value:" + entry. getValue (). name) ;}}/*** modify */public void testModify () {System. out. println ("Enter the student ID to modify"); Student console = new student (System. in); while (true) {String studentId = console. next (); Student st = students. get (studentId); if (st = null) {System. out. println ("this student ID does not exist"); continue;} System. out. println ("current student ID, corresponding to the student" + st. name); System. out. println ("enter the name of the new student:"); String studentName = console. next (); students. put (studentId, new Student (studentId, studentName); System. out. println ("modified successfully"); break;} public static void main (String [] args) {MapTest mt = new MapTest (); mt. testPut (); mt. testKeySet (); mt. testRemove (); mt. testEntrySet (); mt. testModify (); mt. testEntrySet ();}}

Execution result:

Enter student ID: 1 Enter Student name: Tom successfully add Student: Tom enter student ID: 2 enter Student name: Jack successfully add Student: Jack enter student ID: 3 enter Student name: lucy successfully added Student: Lucy has a total of 3 student students: Tom Student: Jack Student: Lucy please enter the ID of the student to be deleted: 12 The student ID does not exist. Enter the student ID to be deleted: 3. The student ID is successfully deleted: Lucy.
There are a total of 2 student key values: 1 corresponding value: Tomkey value: 2 corresponding value: Jack

Enter the student ID to modify
2
Current student ID. The student ID is Jack.
Enter the name of the new student:
Lisa
Modified successfully
Total 2 students
Key Value: 1
Corresponding value: Tom
Key Value: 2
The corresponding value is Lisa.

 

 

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.