首页 > 技术文章 > Java入门第三季——Java中的集合框架(中):Map&HashMap

songsongblue 2018-10-11 14:48 原文

 

 

 

 

 

 

 

 

 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 }

 

推荐阅读