轉自:https://www.imooc.com/video/4039/0
僅供個人學習記錄,侵刪
1 Map中查看是否包含某個key用containsKey、查看是否包含某個值用containsValue
直接上代碼
public void testContains(){ System.out.println(""); System.out.println("查看是否包含某學生,請輸入ID:"); Scanner console = new Scanner(System.in); String id = console.next(); if(students.containsKey(id)){ Student st = students.get(id); System.out.println("該學生存在,姓名為:"+st.name); }else{ System.out.println("不存在該學生"); } //containsValue System.out.println("查看是否包含某學生,請輸入學生姓名:"); String stName = console.next(); Student s1 = new Student(1,stName); if(students.containsValue(s1)){ System.out.println("該學生存在,姓名為:"+s1.name); }else{ System.out.println("該學生不存在"); } }
MapTest類如下

1 package com.collection; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 import java.util.Map.Entry; 6 import java.util.Set; 7 import java.util.Scanner; 8 9 public class MapTest { 10 11 public HashMap<String,Student> students = new HashMap<String,Student>(); 12 13 /* 14 * 新建學生到Map中 15 * */ 16 public void addStudent(){ 17 //先添加三個學生 18 Scanner console = new Scanner(System.in); 19 int i = 0; 20 while(i<3){ 21 System.out.println("請輸入學生ID:"); 22 String id = console.next(); 23 Student s = students.get(id); 24 if(s == null){ 25 System.out.println("請輸入學生姓名:"); 26 String name = console.next(); 27 Student student = new Student(Integer.parseInt(id),name); 28 students.put(id,student); 29 System.out.println("添加了學生:"+student.id+"-"+student.name); 30 i++; 31 }else{ 32 System.out.println("該ID已經被占用"); 33 continue; 34 } 35 } 36 } 37 38 39 /* 40 * 試用HashMap的keySet方法 41 * 42 * 順便遍歷Students 43 * */ 44 public void forEachStudents(){ 45 Set<String> ks = students.keySet(); 46 System.out.println("共有學生數量"+students.size()+"個,具體如下:"); 47 for(String key: ks){ 48 Student student = students.get(key); 49 if( student != null){ 50 System.out.println("學生ID:"+student.id+"-學生姓名:"+student.name); 51 } 52 } 53 } 54 55 /* 56 * set的刪除 57 * */ 58 public void testDel(){ 59 System.out.println(""); 60 Scanner console = new Scanner(System.in); 61 while(true){ 62 System.out.println("請輸入需要刪除的學生的ID"); 63 String id = console.next(); 64 Student st = students.get(id); 65 if(st == null){ 66 System.out.println("該ID不存在"); 67 continue; 68 } 69 students.remove(id); 70 System.out.println("成功刪除學生:"+st.name); 71 break; 72 } 73 } 74 75 /* 76 * Entry方法遍歷Set 77 * 78 * */ 79 public void entryForEach(){ 80 System.out.println(""); 81 System.out.println("Entry 方法遍歷數據"); 82 Set<Entry<String,Student>> entrySet = students.entrySet(); 83 for(Entry<String,Student> entry:entrySet){ 84 System.out.println("取得鍵:"+entry.getKey()); 85 System.out.println("取得對應的值:"+entry.getValue().name); 86 } 87 } 88 89 /* 90 * Set更新操作 91 * */ 92 public void testModify(){ 93 System.out.println(""); 94 System.out.println("嘗試更新操作"); 95 Scanner console = new Scanner(System.in); 96 while(true){ 97 System.out.println("請輸入學生ID:"); 98 String id = console.next(); 99 Student st = students.get(id); 100 if(st == null){ 101 System.out.println("無效的ID"); 102 continue; 103 } 104 System.out.println("請輸入學生名字:"); 105 String stName = console.next(); 106 Student newSt = new Student(Integer.parseInt(id),stName); 107 students.put(id,newSt); 108 System.out.println("添加成功"); 109 break; 110 } 111 } 112 113 /* 114 * containsValue 115 * containsKey 116 * 117 * */ 118 public void testContains(){ 119 System.out.println(""); 120 System.out.println("查看是否包含某學生,請輸入ID:"); 121 Scanner console = new Scanner(System.in); 122 String id = console.next(); 123 if(students.containsKey(id)){ 124 Student st = students.get(id); 125 System.out.println("該學生存在,姓名為:"+st.name); 126 }else{ 127 System.out.println("不存在該學生"); 128 } 129 130 //containsValue 131 System.out.println("查看是否包含某學生,請輸入學生姓名:"); 132 String stName = console.next(); 133 Student s1 = new Student(1,stName); 134 if(students.containsValue(s1)){ 135 System.out.println("該學生存在,姓名為:"+s1.name); 136 }else{ 137 System.out.println("該學生不存在"); 138 } 139 140 } 141 142 public static void main(String[] args){ 143 MapTest mt = new MapTest(); 144 mt.addStudent(); 145 mt.forEachStudents(); 146 /*mt.testDel(); 147 mt.entryForEach(); 148 mt.testModify(); 149 mt.entryForEach();*/ 150 mt.testContains(); 151 } 152 }
其中Student類如下:

1 package com.collection; 2 3 import java.util.HashSet; 4 import java.util.Objects; 5 import java.util.Set; 6 7 public class Student { 8 public int id; 9 public String name; 10 11 //set中添加某個對象無論添加多少次,最終只會保留一個該對象(的引用),並且,保留的是第一次添加的那個 12 13 public Set<Course> course = new HashSet<Course>(); 14 15 public Student(int id, String name){ 16 this.id = id; 17 this.name = name; 18 } 19 20 21 @Override 22 public boolean equals(Object o) { 23 if (this == o) return true; 24 if (o == null || getClass() != o.getClass()) return false; 25 26 Student student = (Student) o; 27 28 return name.equals(student.name); 29 } 30 31 @Override 32 public int hashCode() { 33 return name.hashCode(); 34 } 35 }
Student類的equals方法和hashCode方法需要重寫,只比較學生名稱,否則contaiValue會返回false