Java 之集合架構 中(10)

來源:互聯網
上載者:User

標籤:ati   基於   資訊   for   調用   學生   move   news   成功   

Map和HashMap

Map介面:
Map 提供了一種映射關係,其中的元素是以索引值對(Key-Value)的形式
儲存的,能夠實現根據Key快速尋找value
Map中的索引值對以Evtry類型的對象執行個體形式存在
索引值(Key) 不可重複,value值可以重複
每個鍵最多隻能映射到一個值
Map介面提供了返回key值集合,value集合以及Entry(索引值對)集合的方法
Map支援泛型,形式如:Map<K,V>

 

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

 

 

案列功能說明

通過Map<String,Student>進行學生資訊管理
其中key值為學生的ID,value為學生的對象

  通過鍵盤輸入學生資訊

  對集合中的學生資訊進行增,刪,改,查操作

 

  1 import java.util.HashMap;  2 import java.util.Map;  3 import java.util.Map.Entry;  4 import java.util.Scanner;  5 import java.util.Set;  6   7 public class MapTest {  8   9     // 用來承裝學生類型對象  // 這裡的Student為上次寫的Student類 (參考Java之集合架構上(9)) 10     public Map<String, Student> students; 11     // 在構造器中初始化students屬性 12     public MapTest(){ 13         this.students = new HashMap<String, Student>(); 14     } 15      16     /* 17      * 測試添加:輸入學生ID,判斷是否被佔用 18      * 若未被佔用,則輸入姓名,建立學生對象,並且 19      * 添加到students中 20      */ 21     public void testPut(){ 22         // 建立一個Scanner對象,用來擷取輸入的學生ID和姓名 23         Scanner console = new Scanner(System.in); 24         int i = 0; 25         while (i < 3){ 26             System.out.println("請輸入學生ID:"); 27             String ID = console.next(); 28             // 判斷ID是否被佔用 29             Student st = students.get(ID); 30             if (st == null){ 31                 // 提示輸入學生姓名 32                 System.out.println("請輸入學生姓名:"); 33                 String name = console.next(); 34                 // 建立新的學生對象 35                 Student newStudent = new Student(ID,name); 36                 // 通過調用students的put方法,添加ID-學生映射 37                 students.put(ID, newStudent); 38                 System.out.println("成功新增學生:" + students.get(ID).name); 39                 i ++; 40             } 41             else{ 42                 System.out.println("該學生ID已被佔用!"); 43                 continue; 44             } 45         } 46     } 47      48     // 測試Map的KeySet方法 49     public void testKeySet(){ 50         // 通過KeySet方法,返回Map中的所有“鍵”的Set集合 51         Set<String> KeySet = students.keySet(); 52         // 取得students的容量 53         System.out.println("總共有:" + students.size() + "個學生!"); 54         // 遍曆KeySet,取得每一個鍵,在調用get方法取得每一個鍵對應的value值 55         for (String stuId: KeySet){ 56             Student st = students.get(stuId) ; 57             if (st != null){ 58                 System.out.println("學生:" + st.name); 59             } 60         } 61     } 62      63     // 測試刪除Map中已有的映射 64     public void testRemove(){ 65         // 擷取從鍵盤輸入的待刪除學生ID字串 66         Scanner console = new Scanner(System.in); 67         while(true){ 68             // 提示輸入待刪除的學生的ID 69             System.out.println("請輸入要刪除的學生ID!"); 70             String ID = console.next(); 71             // 判斷ID是否有對應的學生對象 72             Student st = students.get(ID); 73             if (st == null){ 74                 // 提示輸入的ID並不存在 75                 System.out.println("該ID不存在!"); 76                 continue; 77             } 78             students.remove(ID); 79             System.out.println("成功刪除學生:" + st.name); 80             break; 81         } 82     } 83      84     // 通過entrySet方法來遍曆Map 85     public void testEntrySet(){ 86         // 通過entrySet方法,返回Map中的所有索引值對 87         Set<Entry <String,Student>> entrySet = students.entrySet(); 88         for (Entry<String, Student> entry:entrySet){ 89             System.out.println("取得鍵:" + entry.getKey()); 90             System.out.println("對應的值:" + entry.getValue().name); 91              92         } 93     } 94      95     // 利用put方法修改Map中已有映射 96     public void testModify(){ 97         // 提示輸入要修改的學生ID 98         System.out.println("請輸入要修改的學生ID:"); 99         // 建立一個Scanner對象,去擷取從鍵盤上輸入的學生ID字串100         Scanner console = new Scanner(System.in);101         while (true ){102             // 取得從鍵盤輸入的學生ID103             String stuID = console.next();104             // 從students中尋找該學生ID對應學生對象105             Student student = students.get(stuID);106             if (student == null){107                 System.out.println("該ID不存在!請從新輸入!");108                 continue;109             }110             // 提示當前對應學生對象的姓名111             System.out.println("當前學生ID,所對應的學生為:" + student.name);112             // 提示輸入新的學生姓名,來修改已有映射113             System.out.println("請輸入新的學生姓名:");114             String name = console.next();115             Student newStudent = new Student(stuID,name);116             students.put(stuID, newStudent);117             System.out.println("修改成功!");118             break;119         }120     }121     122     public static void main(String[] args) {123 124         MapTest mt = new MapTest();125         mt.testPut();126         mt.testKeySet();127         mt.testRemove();128         mt.testEntrySet();129         mt.testModify();130         mt.testEntrySet();131     }132 133 }

 

Java 之集合架構 中(10)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.