mapmap中的key可以重复吗吗

所有Map操作中key的值是不能重复的,例如,HashMap操作时key是不能重复的,如果重复则肯定会覆盖之前的内容,如下代码所示。
范例:Map中的key不允许重复,重复就是覆盖
程序运行结果:
从程序的运行结果中可以发现,第二个内容覆盖了第一个内容,所以此时可以使用Identity HashMap。使用此类时只要地址不相等(key1!=key2),就表示不是重复的key,可以添加到集合中。
范例:使用IdentityHashMap修改程序
程序运行结果:
从程序的运行结果中可以发现,现在的key允许重复,只要两个对象的地址不相等即可。
阅读(...) 评论()有两个MAP,KEY都相同,只有VALUE不同,要做到将VALUE不同的KEY找出来,
yurongfu3544
试试import java.util.HashMimport java.util.Iimport java.util.Mimport java.util.Spublic class $ {public static void main(String[] args) {Map map0 = new HashMap();Map map1 = new HashMap();map0.put("a",1);map0.put("b",2);map0.put("c",3);map1.put("a",11);map1.put("b",12);map1.put("c",13);map1.put("d",14);Set set = map0.keySet();Iterator it = set.iterator();while (it.hasNext()) {String key = it.next();Integer val0 = map0.get(key);Integer val1 = map1.get(key);if (val0 != val1) {System.out.println(key);}}}}结果:bca
为您推荐:
其他类似问题
扫描下载二维码The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.

我要回帖

更多关于 eclipse官网 的文章

 

随机推荐