• JAVA高级教程-Java Map(6)



    6、Map的使用

    package Map01;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * Map接口的使用
     */
    public class Demo01_HashMap {
    
        public static void main(String[] args) {
            Map<String,String> map=new HashMap<>();
            map.put("cn","中国");
            map.put("uk","英国");
            map.put("usa","美国");
            map.put("usa","美国1");
    
            System.out.println("数据的个数"+map.size());
            System.out.println(map.toString());
    
    
            //删除
            map.remove("usa");
    
    
            //遍历
            //使用keyset
            System.out.println("============使用keyset======================");
    //        Set keys= map.keySet();
            for (String key : map.keySet()) {
                System.out.println(key+":"+map.get(key));
            }
    
            //使用entryset遍历
            System.out.println("============使用entryset遍历======================");
    //        Set> entries=map.entrySet();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                System.out.println(entry.getKey()+":"+entry.getValue());
    
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    public static void main(String[] args) {
            HashMap<Students,String> stu=new HashMap<>();
            Students p1=new Students("刘德华",20);
            Students p2=new Students("李小龙",22);
            Students p3=new Students("彭于晏",24);
    
            System.out.println("数据的个数"+stu.size());
            System.out.println(stu.toString());
    
            stu.put(p1,"上海");
            stu.put(p2,"北京");
            stu.put(p3,"深圳");
            //重写了hascode就不会重读
            stu.put(new Students("彭于晏",24),"深圳");
    
    
            System.out.println("数据的个数"+stu.size());
            System.out.println(stu.toString());
    
    
            //删除
            stu.remove(p1);
    
    
            //遍历
            //使用keyset
            System.out.println("============使用keyset======================");
    //        Set keys= map.keySet();
            for (Students key : stu.keySet()) {
                System.out.println(key+":"+stu.get(key));
            }
    
            //使用entryset遍历
            System.out.println("============使用entryset遍历======================");
    //        Set> entries=map.entrySet();
            for (Map.Entry<Students, String> entry : stu.entrySet()) {
                System.out.println(entry.getKey()+":"+entry.getValue());
    
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
  • 相关阅读:
    若依微服务如何处理Long类型精度丢失问题?
    【JavaEE】MyBaits(注解方式实现映射)
    33.2.2 安装Mycat状态检查服务
    pandas Series 使用整数索引的一些问题
    【文本分析】(三)
    【Android】ADB无线连接Android设备
    电子统计台账:数据感知与模板找错
    如何使用csproj构建C#源代码组件NuGet包?
    童心智造2022.09.05线段树练习
    C++异常
  • 原文地址:https://blog.csdn.net/Leoon123/article/details/133969632