zoukankan      html  css  js  c++  java
  • Java集合中Map接口的使用方法

    Map接口

    • Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value;
    • Map中的键值对以Entry类型的对象实例形式存在;
    • 建(key值)不可重复,value值可以重复,一个value值可以和很多key值形成对应关系,每个建最多只能映射到一个值。
    • Map支持泛型,形式如:Map<K,V>
    • Map中使用put(K key,V value)方法添加


    HashMap类

    • HashMap是Map的一个重要实现类,也是最常用的,基于哈希表实现
    • HashMap中的Entry对象是无序排列的
    • Key值和value值都可以为null,但是一个HashMap只能有一个key值为null的映射(key值不可重复)

    下面我们以添加学生对象的实例演示Map的用法。

    首先,创建一个学生类

     1 import java.util.HashSet;
     2 import java.util.Set;
     3 
     4 /**
     5  * 学生类
     6  * @author lenovo
     7  *
     8  */
     9 public class Student {
    10 
    11     public String id;
    12     
    13     public String name;
    14     
    15     public Set<KeCheng> kecheng;
    16     
    17     public Student(String id,String name){
    18         this.id = id;
    19         this.name = name;
    20         this.kecheng = new HashSet<KeCheng>();
    21     }
    22     
    23 }

    然后再创建一个MapTest的测试类,演示Map的使用方法,并且创建一个演示put()方法和keySet()方法的成员方法

     1 import java.util.HashMap;
     2 import java.util.Map;
     3 import java.util.Scanner;
     4 import java.util.Set;
     5 
     6 public class MapTest {
     7 
     8     //创建一个Map属性用来承装学生对象
     9     public Map<String,Student> student;
    10     
    11     /*
    12      * 在构造器中初始化学生属性
    13      */
    14     public MapTest(){
    15         this.student = new HashMap<String,Student>();
    16     }
    17     
    18     /*
    19      * 添加方法:输入学生ID,判断是否被占用,
    20      * 如果未被占用,则输入姓名,创建新学生对象,添加到student中
    21      */
    22     public void testPut(){
    23         //创建一个Scanner对象,输入学生ID
    24         Scanner sc = new Scanner(System.in);
    25         int i = 0;
    26         while(i<3){
    27             System.out.println("请输入学生ID:");
    28             String stuID = sc.next();
    29             Student stu = student.get(stuID);
    30             if(stu == null){
    31                 System.out.println("请输入学生姓名:");
    32                 String stuName = sc.next();
    33                 Student newStudent = new Student(stuID,stuName);
    34                 student.put(stuID, newStudent);
    35                 System.out.println("成功添加学生:"+student.get(stuID).name);
    36                 i++;
    37             }else{
    38                 System.out.println("该学生ID已被占用!");
    39                 continue;
    40             }
    41             
    42         }
    43     }
    44     
    45     /*
    46      * 测试Map的keySet方法
    47      */
    48     public void testKeySet(){
    49         //通过keySet方法,返回Map中所有“键”的Set集合
    50         Set<String> keySet = student.keySet();
    51         //取得student的容量
    52         System.out.println("总共有"+student.size()+"个学生;");
    53         //遍历keySet,取得每一个键,再调用get方法取得每个键对应的value
    54         for (String stuID : keySet) {
    55             Student stu = student.get(stuID);
    56             if(stu != null){
    57                 System.out.println("学生:"+stu.name);
    58             }
    59         }
    60     }
    61     
    62     public static void main(String[] args) {
    63 
    64         MapTest mt = new MapTest();
    65         mt.testPut();
    66         mt.testKeySet();
    67     }
    68 
    69 }

    运行main方法后的结果如下:

    请输入学生ID:
    1
    请输入学生姓名:
    Tom
    成功添加学生:Tom
    请输入学生ID:
    2
    请输入学生姓名:
    Jack
    成功添加学生:Jack
    请输入学生ID:
    3
    请输入学生姓名:
    Lily
    成功添加学生:Lily
    总共有3个学生;
    学生:Tom
    学生:Jack
    学生:Lily

    使用Map中的remove()方法删除Map中的映射

     1 /*
     2      * 删除Map中的映射
     3      */
     4     public void testRemove(){
     5         Scanner sc = new Scanner(System.in);
     6         while(true){
     7             System.out.println("请输入要删除的学生ID:");
     8             String stuID = sc.next();
     9             //判断输入的ID是否存在对应的学生对象
    10             Student stu = student.get(stuID);
    11             if(stu == null){
    12                 System.out.println("输入的学生ID不存在!");
    13                 continue;
    14             }
    15             student.remove(stuID);
    16             System.out.println("成功删除学生"+stu.name);
    17             break;
    18         }
    19         testEntrySet();
    20     }

     使用entrySet()方法遍历Map

     1     /*
     2      * 通过entrySet来遍历Map
     3      */
     4     public void testEntrySet(){
     5         //通过entrySet返回Map中所有的键值对
     6         Set<Entry<String,Student>> entrySet = student.entrySet();
     7         for(Entry<String,Student> entry:entrySet){
     8             System.out.println("取得键:"+entry.getKey());
     9             System.out.println("对应的值为:"+entry.getValue().name);
    10         }
    11     }

    使用put()方法来修改Map中已存在的映射

     1 /*
     2      * 使用put方法修改Map中已有的映射
     3      */
     4     public void testModify(){
     5         System.out.println("请输入要修改的学生ID:");
     6         Scanner sc = new Scanner(System.in);
     7         while(true){
     8             String id = sc.next();
     9             Student stu = student.get(id);
    10             if(stu == null){
    11                 System.out.println("ID不存在!");
    12                 continue;
    13             }
    14             System.out.println("当前学生是:"+stu.name);
    15             System.out.println("请输入新的学生:");
    16             String name = sc.next();
    17             Student newStu = new Student(id,name);
    18             student.put(id, newStu);
    19             System.out.println("修改成功!");
    20             break;
    21         }
    22     }

    使用Map中的containsKey()和containsValue()方法来判断Map中是否存在键或值

     1 /*
     2      * 测试Map中是否存在某个key值或value值
     3      */
     4     public void testContainsKey(){
     5         System.out.println("请输入学生ID:");
     6         Scanner sc = new Scanner(System.in);
     7         String stuID = sc.next();
     8         //用containsKey()方法来判断是否存在某个key值
     9         System.out.println("输入的ID为:"+stuID+",在学生列表中是否存在:"+student.containsKey(stuID));
    10         if(student.containsKey(stuID)){
    11             System.out.println("学生的姓名为:"+student.get(stuID).name);
    12         }
    13         
    14         System.out.println("请输入学生姓名:");
    15         String name = sc.next();
    16         //用containsValue()方法来判断是否存在某个value值
    17         if(student.containsValue(new Student(null,name))){
    18             System.out.println("存在学生"+name);
    19         }else{
    20             System.out.println("学生不存在");
    21         }
    22     }

    使用containsKey()和containsValue()方法判断是,先在学生类里重写equals()和hashCode()方法,如果只判断值的话,equals方法里只重写和值相关的内容。

  • 相关阅读:
    scala之伴生对象的继承
    scala之伴生对象说明
    “Failed to install the following Android SDK packages as some licences have not been accepted” 错误
    PATH 环境变量重复问题解决
    Ubuntu 18.04 配置java环境
    JDBC的基本使用2
    DCL的基本语法(授权)
    ZJNU 1374
    ZJNU 2184
    ZJNU 1334
  • 原文地址:https://www.cnblogs.com/jpwz/p/5680494.html
Copyright © 2011-2022 走看看