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());
        }
    }
}
相关推荐
暮雪倾风13 分钟前
【JS-Node】node.js环境安装及使用
开发语言·javascript·node.js
Dxy12393102167 小时前
Python 使用正则表达式将多个空格替换为一个空格
开发语言·python·正则表达式
故事和你919 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
techdashen10 小时前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
一 乐10 小时前
电影院|基于springboot + vue电影院购票管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·电影院购票管理管理系统
星空椰10 小时前
JavaScript 进阶基础:函数、作用域与常用技巧总结
开发语言·前端·javascript
恼书:-(空寄10 小时前
JVM GC 日志分析 + 常见 GC 场景 + 实战参数调优
java·jvm
消失的旧时光-194310 小时前
Spring Boot 实战(五):接口工程化升级(统一返回 + 异常处理 + 错误码体系 + 异常流转机制)
java·spring boot·后端·解耦
忒可君10 小时前
C# winform 自制分页功能
android·开发语言·c#
Rust研习社10 小时前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust