博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HashMap的使用方法及注意事项
阅读量:7210 次
发布时间:2019-06-29

本文共 3385 字,大约阅读时间需要 11 分钟。

99、Map(映射):Map 的keySet()方法会返回 key 的集合,因为 Map 的键是不能重复的,因此 keySet()方法的返回类型是 Set;而 Map 的值是可以重复的,因此 values()方法的返回类型是 Collection,可以容纳重复的元素。

100、Map注意事项:

A)

public class TestMap {     publicstatic void main(String[] args) {                HashMap map = new HashMap();                map.put("a","zhangsan");        map.put("b","lisi");        map.put("c","wangwu");        map.put("a","cuijun");                //System.out.println(map);                String value = (String)map.get("a");        System.out.println(value);        System.out.println("---------------------");                Stringvalue1 = (String)map.get("d");        System.out.println(value1);            }}

由上述代码可知不可以重复。

B)

public classTestMap1 {     publicstatic void main(String[] args) {                HashMap map = new HashMap();                String str = newString("cuijun");                map.put("a",str);        map.put("b",str);        //map.put("a", "cuijun");        //map.put("b", "cuijun");                System.out.println(map);    }}

 

由上述代码可知可以重复。

注:因此 keySet()方法的返回类型是 Set,values()方法的返回类型是 Collection。

101、关于HashSet的使用:

A)

public class TestMap2 {     publicstatic void main(String[] args) {                HashMap map = new HashMap();                map.put("a","aa");        map.put("b","bb");        map.put("c","cc");        map.put("d","dd");         Set keys = map.keySet();//使用keySet()方法,获取键的集合                for(Iterator it =keys.iterator(); it.hasNext();){            String key = (String)it.next();            String value = (String)map.get(key);             System.out.println(key+"="+value);        }    }}

 

b)

publicclass TestMap3 {     publicstatic void main(String[] args) {                HashMap map = new HashMap();                map.put("a","aa");        map.put("b","bb");        map.put("c","cc");        map.put("d","dd");                Set keys = map.entrySet();//使用entrySet()方法,获取entry对象的集合                for(Iterator it =keys.iterator(); it.hasNext();){             Map.Entry maps = (Map.Entry)it.next();//Map.Entry:Entry是Map的一个内部类。具体详情请参考API。         /*         * 关于内部类的简单介绍:内部类是定义在类内部的类         * public class A{         *      publicclass B{}         * }         * A a = new A();         * A.B b = new A.B();         */             String key = (String)maps.getKey();            String value = (String)maps.getValue();             System.out.println(key+":"+value);        }    }}

 

附Map.Entry的API简介:

Map.entrySet方法返回映射的 collection视图,其中的元素属于此类。获得映射项引用的唯一方法是通过此 collection视图的迭代器来实现。这些Map.Entry对象在迭代期间有效;更确切地讲,如果在迭代器返回项之后修改了底层映射,则某些映射项的行为是不确定的,除了通过setValue在映射项上执行操作之外。

C)通过使用HashSet判断输入值的个数

publicclassTestMap4 {     publicstatic void main(String[] args) {                HashMap map = new HashMap();                for(int i = 0; i < args.length; i++){                        if(map.get(args[i]) ==null){                map.put(args[i],newInteger(1));            }else{                Integer value = (Integer)map.get(args[i]);                value =  new Integer(value.intValue()+1);                map.put(args[i],value);            }        }                Set keys = map.keySet();                for(Iterator it =keys.iterator(); it.hasNext();){            String key = (String)it.next();            Integer value = (Integer)map.get(key);            System.out.println(key+"="+value);        }    }}

 

  =====================关于HashSet其实运行下这些代码,参考着API是很容易理解的=============

 

转载地址:http://cerum.baihongyu.com/

你可能感兴趣的文章
在Oracle中查询存储过程和函数
查看>>
关于对象序列化json 说说
查看>>
设置div滚动条
查看>>
git常用命令
查看>>
React全家桶写一个CNode社区,奉上心得与源码
查看>>
全局替换字体,开源库更方便!!!
查看>>
Spring Cloud构建微服务架构:消息驱动的微服务(消费分区)【Dalston版】
查看>>
数据库路由中间件MyCat - 使用篇(4)
查看>>
JavaScript 异步队列实现及拓展
查看>>
三个白帽之从pwn me调试到Linux攻防学习
查看>>
JIT引擎触发RowHammer可行性研究
查看>>
朴素贝叶斯算法文本分类原理
查看>>
笔记-GPUImage(二)图片滤镜
查看>>
Spark综合使用及用户行为案例页面转化率统计分析实战-Spark商业应用实战
查看>>
Android Studio 3.2.0 正式版新特性
查看>>
JavaScript_JS里的函数:值和闭包
查看>>
Swift中遇到的警告--解决方法
查看>>
微信小程序学习笔记 路由传参
查看>>
Java 枚举查找并不抛异常的实现
查看>>
我与前端 | 视野的重要性
查看>>