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