HashMap的运用小练习

java 复制代码
    public static void main(String[] args) {
        HashMap hashMap = new HashMap();
        hashMap.put("jack",650);
        hashMap.put("tom",1200);
        hashMap.put("smith",2900);

        System.out.println(hashMap);
        //将jack的工资更改为2600
        hashMap.put("jack",2600);
        System.out.println(hashMap);
        Set entrySet = hashMap.entrySet();

        //将每个价钱自增100员
        System.out.println("=========自增100元之后的薪水========");
        Set setkey = hashMap.keySet();
        for (Object key :setkey) {
            hashMap.put(key,(Integer)hashMap.get(key)+100);
            /**将hashmap.get()的类型为:java.lang.Class<capture#1, 共 ? extends java.lang.Object>
             * 需要向下转型为Integer才能与int类型的数字相加
             */
        }

        System.out.println(hashMap);

        //遍历集合的key值
        System.out.println("=====遍历集合的key值====");
        Iterator iterator1 = hashMap.keySet().iterator();
        while (iterator1.hasNext()) {
            Object next =  iterator1.next();
            System.out.println(next);
        }


        //使用values遍历集合的values值
        System.out.println("=====遍历集合的values值=====");
        Iterator iterator = hashMap.values().iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();
            System.out.println(next);
        }

        //使用Entry遍历集合的values值
        System.out.println("======使用Entry遍历集合的values值=====");
        Set entryset = entrySet;
        Iterator iterator2 = entryset.iterator();
        while (iterator2.hasNext()){
//            Object next = iterator2.next();
            Map.Entry next = (Map.Entry)iterator2.next();
            System.out.println(next.getValue()+" "+next.getKey());
        }
    }
}
相关推荐
励志不掉头发的内向程序员8 分钟前
STL库——string(类模拟实现)
开发语言·c++
王廷胡_白嫖帝10 分钟前
Qt文件压缩工具项目开发教程
java·开发语言·qt
张飞洪30 分钟前
C# 13 与 .NET 9 跨平台开发实战:基于.NET 9 与 EF Core 9 的现代网站与服务开发
开发语言·c#·.net
渣哥35 分钟前
Java开发必看!序列化与反序列化到底有多重要?
java
郝学胜-神的一滴36 分钟前
使用C++11改进工厂方法模式:支持运行时配置的增强实现
开发语言·c++·程序人生·设计模式
仙俊红39 分钟前
使用jwt+redis实现单点登录
java
我是场42 分钟前
Android14内核调试 - boot & vendor_boot
java·开发语言·spring boot
爱和冰阔落43 分钟前
从关机小游戏学 C 语言:分支循环 + 关键字(break/continue)实战
c语言·开发语言
Korloa1 小时前
表达式(CSP-J 2021-Expr)题目详解
c语言·开发语言·数据结构·c++·算法·蓝桥杯·个人开发
fake_ss1981 小时前
从零开发Java坦克大战Ⅱ (下)-- 从单机到联机(完整架构功能实现)
java·网络·tcp/ip·游戏程序