1. 程式人生 > >Map介面的理解和使用

Map介面的理解和使用

轉自:https://www.cnblogs.com/jpwz/p/5680494.html

Java集合中Map介面的使用方法

Map介面

  • Map提供了一種對映關係,其中的元素是以鍵值對(key-value)的形式儲存的,能夠實現根據key快速查詢value;
  • Map中的鍵值對以Entry型別的物件例項形式存在;
  • 建(key值)不可重複,value值可以重複,一個value值可以和很多key值形成對應關係,每個建最多隻能對映到一個值。
  • Map支援泛型,形式如:Map<K,V>
  • Map中使用put(K key,V value)方法新增


HashMap類

  • HashMap是Map的一個重要實現類,也是最常用的,基於雜湊表實現
  • HashMap中的Entry物件是無序排列的
  • Key值和value值都可以為null,但是一個HashMap只能有一個key值為null的對映(key值不可重複)

下面我們以新增學生物件的例項演示Map的用法。

首先,建立一個學生類

複製程式碼

 1 import java.util.HashSet;
 2 import java.util.Set;
 3 
 4 /**
 5  * 學生類
 6  * @author lenovo
 7  *
 8  */
 9 public class Student {
10 
11     public String id;
12     
13     public String name;
14     
15     public Set<KeCheng> kecheng;
16     
17     public Student(String id,String name){
18         this.id = id;
19         this.name = name;
20         this.kecheng = new HashSet<KeCheng>();
21     }
22     
23 }

複製程式碼

 

然後再建立一個MapTest的測試類,演示Map的使用方法,並且建立一個演示put()方法和keySet()方法的成員方法

複製程式碼

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 import java.util.Scanner;
 4 import java.util.Set;
 5 
 6 public class MapTest {
 7 
 8     //建立一個Map屬性用來承裝學生物件
 9     public Map<String,Student> student;
10     
11     /*
12      * 在構造器中初始化學生屬性
13      */
14     public MapTest(){
15         this.student = new HashMap<String,Student>();
16     }
17     
18     /*
19      * 新增方法:輸入學生ID,判斷是否被佔用,
20      * 如果未被佔用,則輸入姓名,建立新學生物件,新增到student中
21      */
22     public void testPut(){
23         //建立一個Scanner物件,輸入學生ID
24         Scanner sc = new Scanner(System.in);
25         int i = 0;
26         while(i<3){
27             System.out.println("請輸入學生ID:");
28             String stuID = sc.next();
29             Student stu = student.get(stuID);
30             if(stu == null){
31                 System.out.println("請輸入學生姓名:");
32                 String stuName = sc.next();
33                 Student newStudent = new Student(stuID,stuName);
34                 student.put(stuID, newStudent);
35                 System.out.println("成功新增學生:"+student.get(stuID).name);
36                 i++;
37             }else{
38                 System.out.println("該學生ID已被佔用!");
39                 continue;
40             }
41             
42         }
43     }
44     
45     /*
46      * 測試Map的keySet方法
47      */
48     public void testKeySet(){
49         //通過keySet方法,返回Map中所有“鍵”的Set集合
50         Set<String> keySet = student.keySet();
51         //取得student的容量
52         System.out.println("總共有"+student.size()+"個學生;");
53         //遍歷keySet,取得每一個鍵,再呼叫get方法取得每個鍵對應的value
54         for (String stuID : keySet) {
55             Student stu = student.get(stuID);
56             if(stu != null){
57                 System.out.println("學生:"+stu.name);
58             }
59         }
60     }
61     
62     public static void main(String[] args) {
63 
64         MapTest mt = new MapTest();
65         mt.testPut();
66         mt.testKeySet();
67     }
68 
69 }

複製程式碼

 

執行main方法後的結果如下:

複製程式碼

請輸入學生ID:
1
請輸入學生姓名:
Tom
成功新增學生:Tom
請輸入學生ID:
2
請輸入學生姓名:
Jack
成功新增學生:Jack
請輸入學生ID:
3
請輸入學生姓名:
Lily
成功新增學生:Lily
總共有3個學生;
學生:Tom
學生:Jack
學生:Lily

複製程式碼

使用Map中的remove()方法刪除Map中的對映

複製程式碼

 1 /*
 2      * 刪除Map中的對映
 3      */
 4     public void testRemove(){
 5         Scanner sc = new Scanner(System.in);
 6         while(true){
 7             System.out.println("請輸入要刪除的學生ID:");
 8             String stuID = sc.next();
 9             //判斷輸入的ID是否存在對應的學生物件
10             Student stu = student.get(stuID);
11             if(stu == null){
12                 System.out.println("輸入的學生ID不存在!");
13                 continue;
14             }
15             student.remove(stuID);
16             System.out.println("成功刪除學生"+stu.name);
17             break;
18         }
19         testEntrySet();
20     }

複製程式碼

 

 使用entrySet()方法遍歷Map

複製程式碼

 1     /*
 2      * 通過entrySet來遍歷Map
 3      */
 4     public void testEntrySet(){
 5         //通過entrySet返回Map中所有的鍵值對
 6         Set<Entry<String,Student>> entrySet = student.entrySet();
 7         for(Entry<String,Student> entry:entrySet){
 8             System.out.println("取得鍵:"+entry.getKey());
 9             System.out.println("對應的值為:"+entry.getValue().name);
10         }
11     }

複製程式碼

使用put()方法來修改Map中已存在的對映

複製程式碼

 1 /*
 2      * 使用put方法修改Map中已有的對映
 3      */
 4     public void testModify(){
 5         System.out.println("請輸入要修改的學生ID:");
 6         Scanner sc = new Scanner(System.in);
 7         while(true){
 8             String id = sc.next();
 9             Student stu = student.get(id);
10             if(stu == null){
11                 System.out.println("ID不存在!");
12                 continue;
13             }
14             System.out.println("當前學生是:"+stu.name);
15             System.out.println("請輸入新的學生:");
16             String name = sc.next();
17             Student newStu = new Student(id,name);
18             student.put(id, newStu);
19             System.out.println("修改成功!");
20             break;
21         }
22     }

複製程式碼

使用Map中的containsKey()和containsValue()方法來判斷Map中是否存在鍵或值

複製程式碼

 1 /*
 2      * 測試Map中是否存在某個key值或value值
 3      */
 4     public void testContainsKey(){
 5         System.out.println("請輸入學生ID:");
 6         Scanner sc = new Scanner(System.in);
 7         String stuID = sc.next();
 8         //用containsKey()方法來判斷是否存在某個key值
 9         System.out.println("輸入的ID為:"+stuID+",在學生列表中是否存在:"+student.containsKey(stuID));
10         if(student.containsKey(stuID)){
11             System.out.println("學生的姓名為:"+student.get(stuID).name);
12         }
13         
14         System.out.println("請輸入學生姓名:");
15         String name = sc.next();
16         //用containsValue()方法來判斷是否存在某個value值
17         if(student.containsValue(new Student(null,name))){
18             System.out.println("存在學生"+name);
19         }else{
20             System.out.println("學生不存在");
21         }
22     }

複製程式碼

使用containsKey()和containsValue()方法判斷是,先在學生類裡重寫equals()和hashCode()方法,如果只判斷值的話,equals方法裡只重寫和值相關的內容。