JAVA集合框架之Map

一  Map特性:

  1 Map提供一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value;

  2 Map中键值对以Entry类型的对象实例形式存在;

  3 键,即key不可重复,但是value值可以;

  4 每个键最多只能映射一个值;

  5 Map接口提供了分别返回key值集合、value值集合以及Entry(键值对)集合的方法;

  6 Map支持泛型,形式如:Map<K,V>

二  HashMap类:

  1 HashMap是Map的一个重要实现类,也是最常用的,基于哈希表实现;

  2 HashMap中的Entry对象是无序排列的;

  3 Key值和Value值都可以为null,但是HashMap中只能有一个Key值为null的映射(key值不可重复);

 

三  添加:

示例:

 1 package com.collection;
 2 
 3 import java.util.HashMap;
 4 import java.util.Set;
 5 import java.util.Scanner;
 6 
 7 public class MapTest {
 8 
 9     public HashMap<String,Student> students = new HashMap<String,Student>();
10 
11     /*
12     * 新建学生到Map中
13     * */
14     public void addStudent(){
15         //先添加三个学生
16         Scanner console = new Scanner(System.in);
17         int i = 0;
18         while(i<3){
19             System.out.println("请输入学生ID:");
20             String id = console.next();
21             Student s = students.get(id);
22             if(s == null){
23                 System.out.println("请输入学生姓名:");
24                 String name = console.next();
25                 Student student = new Student(Integer.parseInt(id),name);
26                 students.put(id,student);
27                 System.out.println("添加了学生:"+student.id+"-"+student.name);
28                 i++;
29             }else{
30                 System.out.println("该ID已经被占用");
31                 continue;
32             }
33         }
34     }
35 
36     /*
37     * 试用HashMap的keySet方法
38     *
39     * 顺便遍历Students
40     * */
41     public void forEachStudents(){
42         Set<String> ks = students.keySet();
43         System.out.println("共有学生数量"+students.size()+"个,具体如下:");
44         for(String key: ks){
45             Student student = students.get(key);
46             if( student != null){
47                 System.out.println("学生ID:"+student.id+"-学生姓名:"+student.name);
48             }
49         }
50     }
51 
52     public static void main(String[] args){
53         MapTest mt = new MapTest();
54         mt.addStudent();
55         mt.forEachStudents();
56     }
57 }
View Code

其中Student类如下:

 1 package com.collection;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 public class Student {
 7     public int id;
 8     public String name;
 9 
10     //set中添加某个对象无论添加多少次,最终只会保留一个该对象(的引用),并且,保留的是第一次添加的那个
11 
12     public Set<Course> course = new HashSet<Course>();
13 
14     public Student(int id, String name){
15         this.id = id;
16         this.name = name;
17     }
18 
19 }
View Code

 

返回结果:

请输入学生ID:
1
请输入学生姓名:
刘备
添加了学生:1-刘备
请输入学生ID:
2
请输入学生姓名:
关羽
添加了学生:2-关羽
请输入学生ID:
3
请输入学生姓名:
张飞
添加了学生:3-张飞
共有学生数量3个,具体如下:
学生ID:1-学生姓名:刘备
学生ID:2-学生姓名:关羽
学生ID:3-学生姓名:张飞
View Code

 

四  删除及Entry方式遍历:


/*
* set的删除
* */
public void testDel(){
System.out.println("");
Scanner console = new Scanner(System.in);
while(true){
System.out.println("请输入需要删除的学生的ID");
String id = console.next();
Student st = students.get(id);
if(st == null){
System.out.println("该ID不存在");
continue;
}
students.remove(id);
System.out.println("成功删除学生:"+st.name);
break;
}
}

/*
* Entry方法遍历Set
*
* */
public void entryForEach(){
System.out.println("");
System.out.println("Entry 方法遍历数据");
Set<Entry<String,Student>> entrySet = students.entrySet();
for(Entry<String,Student> entry:entrySet){
System.out.println("取得键:"+entry.getKey());
System.out.println("取得对应的值:"+entry.getValue().name);
}
}

 

运行结果:

 1 请输入学生ID:
 2 1
 3 请输入学生姓名:
 4 张三
 5 添加了学生:1-张三
 6 请输入学生ID:
 7 2
 8 请输入学生姓名:
 9 李四
10 添加了学生:2-李四
11 请输入学生ID:
12 3
13 请输入学生姓名:
14 王五
15 添加了学生:3-王五
16 共有学生数量3个,具体如下:
17 学生ID:1-学生姓名:张三
18 学生ID:2-学生姓名:李四
19 学生ID:3-学生姓名:王五
20 
21 请输入需要删除的学生的ID
22 4
23 该ID不存在
24 请输入需要删除的学生的ID
25 2
26 成功删除学生:李四
27 
28 Entry 方法遍历数据
29 取得键:1
30 取得对应的值:张三
31 取得键:3
32 取得对应的值:王五
View Code

 

 

四  更新:

/*
    * Set更新操作
    * */
    public void testModify(){
        System.out.println("");
        System.out.println("尝试更新操作");
        Scanner console = new Scanner(System.in);
        while(true){
            System.out.println("请输入学生ID:");
            String id = console.next();
            Student st = students.get(id);
            if(st == null){
                System.out.println("无效的ID");
                continue;
            }
            System.out.println("请输入学生名字:");
            String stName = console.next();
            Student newSt = new Student(Integer.parseInt(id),stName);
            students.put(id,newSt);
            System.out.println("添加成功");
            break;
        }
    }

 

运行结果:

尝试更新操作
请输入学生ID:
5
无效的ID
请输入学生ID:
3
请输入学生名字:
赵四
添加成功

Entry 方法遍历数据
取得键:1
取得对应的值:张三
取得键:3
取得对应的值:赵四

posted @ 2020-04-03 17:53  风铃如沧海  阅读(182)  评论(0编辑  收藏  举报