Chinaunix首页 | 论坛 | 博客
  • 博客访问: 42566
  • 博文数量: 71
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 726
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-24 08:29
文章分类

全部博文(71)

文章存档

2015年(71)

我的朋友

分类: Java

2015-02-16 17:33:00


  1. package com.imooc.collection;

  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5. import java.util.Scanner;
  6. import java.util.Set;

  7. public class MapTest {
  8.     /**
  9.      * 用来承装学生类型对象
  10.      *
  11.      * @param args
  12.      */
  13.     public Map<String, Student> students;

  14.     /**
  15.      * 在构造器中初始化students属性
  16.      *
  17.      * @param args
  18.      */
  19.     public MapTest() {
  20.         this.students = new HashMap<String, Student>();
  21.     }

  22.     /**
  23.      * 测试添加:输入学生ID,判断是否有被占用,若未被占用,则输入姓名,创建新学生对象,并且添加到students中
  24.      *
  25.      * @param args
  26.      */

  27.     public void testPut() {
  28.         // 创建一个 Scanner对象,用来获取输入的学生ID和姓名
  29.         Scanner console = new Scanner(System.in);
  30.         int i = 0;
  31.         while (i < 3) {
  32.             System.out.println("请输入学生ID:");
  33.             String ID = console.next();
  34.             // 判断ID是否被占用
  35.             Student st = students.get(ID);
  36.             if (st == null) {
  37.                 // 提示输入学生姓名
  38.                 System.out.println("请输入学生姓名:");
  39.                 String name = console.next();
  40.                 // 创建新的学生对象
  41.                 Student newStudent = new Student(ID, name);
  42.                 // 通过调用students的put方法,添加ID-学生映射
  43.                 students.put(ID, newStudent);
  44.                 System.out.println("成功添加学生 :" + students.get(ID).name);
  45.                 i++;
  46.             } else {
  47.                 System.out.println("该学生ID已被占用。");
  48.                 continue;
  49.             }
  50.         }
  51.     }

  52.     /**
  53.      * 测试Map的keySet方法
  54.      *
  55.      * @param args
  56.      */

  57.     public void testKeySet() {
  58.         // 通过keySet方法,返回Map中的所有“键”的Set集合
  59.         Set<String> keySet = students.keySet();
  60.         // 取得students的容量
  61.         System.out.println("总共有" + students.size() + "个学生");
  62.         // 遍历keySet,取得每一个键,在调用get方法取得每个键对应的value
  63.         for (String stuId : keySet) {
  64.             Student st = students.get(stuId);
  65.             if (st != null)
  66.                 System.out.println("学生:" + st.name);
  67.         }
  68.     }

  69.     /**
  70.      * 测试删除Map中的映射
  71.      *
  72.      * @param args
  73.      */

  74.     public void testRemove() {

  75.         // 获取从键盘输入的待删除学生ID字符串
  76.         Scanner console = new Scanner(System.in);
  77.         while (true) {
  78.             System.out.println("请输入要删除的学生ID!");
  79.             String ID = console.next();
  80.             // 判断该ID是否有对应的学生对象
  81.             Student st = students.get(ID);
  82.             if (st == null) {
  83.                 // 提示输入的ID并不存在
  84.                 System.out.println("该ID不存在。");
  85.                 continue;
  86.             }
  87.             students.remove(ID);
  88.             System.out.println("成功删除学生:" + st.name);
  89.             break;
  90.         }

  91.     }

  92.     /**
  93.      * 通过entrySet方法来遍历Map
  94.      *
  95.      * @param args
  96.      */

  97.     public void testEntrySet() {
  98.         // 通过entrySet方法返回Map中的所有键值对
  99.         Set<Entry<String, Student>> entrySet = students.entrySet();
  100.         for (Entry<String, Student> entry : entrySet) {
  101.             System.out.println("取得键:" + entry.getKey());
  102.             System.out.println("取得对应的值为:" + entry.getValue().name);
  103.         }
  104.     }

  105.     public static void main(String[] args) {
  106.         MapTest mt = new MapTest();
  107.         mt.testPut();
  108.         mt.testKeySet();
  109.         mt.testRemove();
  110.         mt.testEntrySet();
  111.     }

  112. }

阅读(398) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~