JAVA中Map之containsKey、containsValue

转自: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 }
View Code

其中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 }
View Code

 

Student类的equals方法和hashCode方法需要重写,只比较学生名称,否则contaiValue会返回false

 

posted @ 2020-04-16 15:59  风铃如沧海  阅读(2387)  评论(0编辑  收藏  举报