-
允许Key重复的Map - IdentityHashMap
2015-06-04 20:48:51允许Key重复的Map - IdentityHashMap 2011-11-06 22:28:35 文章转自http://tianya23.blog.51cto.com/1081650/707603 在使用map的时候,大家肯定会想到key-value,key用于检索value的内容。在正常...2011-11-06 22:28:35- class Person{
- private String name ;
- private int age ;
- public Person(String name,int age){
- this.name = name ;
- this.age = age ;
- }
- public boolean equals(Object obj){
- if(this==obj){
- return true ;
- }
- if(!(obj instanceof Person)){
- return false ;
- }
- Person p = (Person)obj ;
- if(this.name.equals(p.name)&&this.age==p.age){
- return true ;
- }else{
- return false ;
- }
- }
- public int hashCode(){
- return this.name.hashCode() * this.age ;
- }
- public String toString(){
- return "姓名:" + this.name + ",年龄:" + this.age ;
- }
- };
- public class IdentityHashMapDemo01{
- public static void main(String args[]){
- Map<Person,String> map = null ; // 声明Map对象
- map = new HashMap<Person,String>() ;
- map.put(new Person("张三",30),"zhangsan_1") ; // 加入内容
- map.put(new Person("张三",30),"zhangsan_2") ; // 加入内容
- map.put(new Person("李四",31),"lisi") ; // 加入内容
- Set<Map.Entry<Person,String>> allSet = null ; // 准备使用Set接收全部内容
- allSet = map.entrySet() ;
- Iterator<Map.Entry<Person,String>> iter = null ;
- iter = allSet.iterator() ;
- while(iter.hasNext()){
- Map.Entry<Person,String> me = iter.next() ;
- System.out.println(me.getKey() + " --> " + me.getValue()) ;
- }
- }
- };
- 姓名:李四,年龄:31 --> lisi
- 姓名:张三,年龄:30 --> zhangsan_2
- public class IdentityHashMapDemo02{
- public static void main(String args[]){
- Map<Person,String> map = null ; // 声明Map对象
- map = new IdentityHashMap<Person,String>() ;
- map.put(new Person("张三",30),"zhangsan_1") ; // 加入内容
- map.put(new Person("张三",30),"zhangsan_2") ; // 加入内容
- map.put(new Person("李四",31),"lisi") ; // 加入内容
- Set<Map.Entry<Person,String>> allSet = null ; // 准备使用Set接收全部内容
- allSet = map.entrySet() ;
- Iterator<Map.Entry<Person,String>> iter = null ;
- iter = allSet.iterator() ;
- while(iter.hasNext()){
- Map.Entry<Person,String> me = iter.next() ;
- System.out.println(me.getKey() + " --> " + me.getValue()) ;
- }
- }
- };
- 姓名:张三,年龄:30 --> zhangsan_2
- 姓名:张三,年龄:30 --> zhangsan_1
- 姓名:李四,年龄:31 --> lisi
-
java map 允许重复_java中key值可以重复的map:IdentityHashMap
2021-02-13 00:07:42在Java中,有一种key值可以重复的map,就是IdentityHashMap。在IdentityHashMap中,判断两个键值k1和 k2相等的条件是 k1 == k2 。在正常的Map 实现(如 HashMap)中,当且仅当满足下列条件时才认为两个键 k1 和 k2 ...在Java中,有一种key值可以重复的map,就是IdentityHashMap。在IdentityHashMap中,判断两个键值k1和 k2相等的条件是 k1 == k2 。在正常的Map 实现(如 HashMap)中,当且仅当满足下列条件时才认为两个键 k1 和 k2 相等:(k1==null ? k2==null : e1.equals(e2))。
IdentityHashMap类利用哈希表实现 Map 接口,比较键(和值)时使用引用相等性代替对象相等性。该类不是 通用 Map 实现!此类实现 Map 接口时,它有意违反 Map 的常规协定,该协定在比较对象时强制使用 equals 方法。此类设计仅用于其中需要引用相等性语义的罕见情况。
在使用IdentityHashMap有些需要注意的地方:
例子1:
IdentityHashMap map =new IdentityHashMap();
map.put(newString("xx"),"first");
map.put(newString("xx"),"second");
for (Entry entry : map.entrySet()) {
System.out.print(entry.getKey() +" ");
System.out.println(entry.getValue());
}
System.out.println("idenMap="+map.containsKey("xx"));
System.out.println("idenMap="+map.get("xx"));
输出结果是:
xx first
xx second
idenMap=false
idenMap=null
例子2:
IdentityHashMap map =new IdentityHashMap();
String fsString =newString("xx");
map.put(fsString,"first");
map.put(newString("xx"),"second");
for(Entry entry : map.entrySet()) {
System.out.print(entry.getKey() +" ");
System.out.println(entry.getValue());
}
System.out.println("idenMap="+map.containsKey(fsString));
System.out.println("idenMap="+map.get(fsString));
输出结果是:
xx second
xx first
idenMap=true
idenMap=first
例子3:
IdentityHashMap map =new IdentityHashMap();
String fsString =newString("xx");
map.put(fsString,"first");
map.put(fsString,"second");
for(Entry entry : map.entrySet()) {
System.out.print(entry.getKey() +" ");
System.out.println(entry.getValue());
}
System.out.println("idenMap="+map.containsKey(fsString));
System.out.println("idenMap="+map.get(fsString));
输出结果是:
xx second
idenMap=true
idenMap=second
例子4:
IdentityHashMap map =new IdentityHashMap();
String fsString =newString("xx");
String secString =newString("xx");
map.put(fsString,"first");
map.put(secString,"second");
for(Entry entry : map.entrySet()) {
System.out.print(entry.getKey() +" ");
System.out.println(entry.getValue());
}
System.out.println("idenMap="+map.containsKey(fsString));
System.out.println("idenMap="+map.get(fsString));
System.out.println("idenMap="+map.containsKey(secString));
System.out.println("idenMap="+map.get(secString));
输出结果是:
xx first
xx second
idenMap=true
idenMap=first
idenMap=true
idenMap=second
例子5:
IdentityHashMap map =new IdentityHashMap();
map.put("xx","first");
map.put("xx","second");
for(Entry entry : map.entrySet()) {
System.out.print(entry.getKey() +" ");
System.out.println(entry.getValue());
}
输出结果是:
xx second
可以看到,在IdentityHashMap中,是判断key是否为同一个对象,而不是普通HashMap的equals方式判断。
参考:http://blog.csdn.net/stoneok07/article/details/7262676
-
java map 允许重复_Java 支持重复key的map
2021-02-28 06:07:47我们平时使用的Map,都是只能在Map中保存一个相同的Key,我们后面保存的相同的key都会将原来的key的值覆盖掉,如下面的例子。public class test {publicstatic void main(String[] args) {String str1 = new String...我们平时使用的Map,都是只能在Map中保存一个相同的Key,我们后面保存的相同的key都会将原来的key的值覆盖掉,如下面的例子。
public class test {
public
static void main(String[] args) {
String str1 = new String("abc");
String str2 = new String("abc");
System.out.println(str1 == str2); //false
Map map = new HashMap();
map.put(str1, "hello");
map.put(str2, "world");
for(Entry entry :map.entrySet())
{
System.out.println(entry.getKey()+" " + entry.getValue());
}
System.out.println("---->" + map.get("abc"));
}
}
这个例子中我们可以看见相同的key只能保存一个value值,下面我们来看一种map可以实现一个key中保存多个value。这个map也就是IdentityHashMap。下面我们就来介绍下IdentityHashMap这个类的使用。
API上这样来解释这个类的:此类不是通用Map实现!此类实现Map接口时,它有意违反Map的常规协定,该协定在比较对象时强制使用
equals 方法。此类设计仅用于其中需要引用相等性语义的罕见情况。
IdentityHashMap类利用哈希表实现Map接口,比较键(和值)时使用引用相等性代替对象相等性。我们来看看这个类的代码吧:
public class test1 {
public
static void main(String[] args) {
String str1 = "abc";
String str2 = "abc";
System.out.println(str1 == str2); //true
Map map = new IdentityHashMap();
map.put(str1, "hello");
map.put(str2, "world");
for(Entry entry : map.entrySet())
{
System.out.println(entry.getKey()+" " + entry.getValue());
}
System.out.println("containsKey---> " +
map.containsKey("abc"));
System.out.println("value----> " + map.get("abc"));
}
}
这端代码输出的结果如下:
true
abc world
containsKey---> true
value----> world
为什么我们的Key还是只保存了一个值????这个问题和《java解惑第62题一样》书上面是这样解释的,我们来看看:
语言规范保证了字符串是内存限定的,换句话说,相等的字符串常量同时也是相同的对象。这可以确保在我们的程序中第二次出现的字符串字面常量“abc”引用到了与第一次相同的String实例上,因此尽管我们使用了一个IdentityHashMap来代替诸如HashMap这样的通用目的的Map实现,但是对程序的行为却不会产生任何影响。
我们来看看下面的代码就可以实现一个key保存两个value的情况。我们的代码如下:
public class test1 {
public
static void main(String[] args) {
String str1 = new String("abc");
String str2 = new String("abc");
System.out.println(str1 == str2); //false
Map map = new IdentityHashMap();
map.put(str1, "hello");
map.put(str2, "world");
for(Entry entry : map.entrySet())
{
System.out.println(entry.getKey()+" " + entry.getValue());
}
System.out.println(" containsKey---> " + map.containsKey("abc"));
System.out.println("str1 containsKey---> " +
map.containsKey(str1));
System.out.println("str2 containsKey---> " +
map.containsKey(str2));
System.out.println(" value----> " + map.get("abc"));
System.out.println("str1 value----> " +
map.get(str1));
System.out.println("str2 value----> " +
map.get(str2));
}
}
我们的看看输出的结果为:
false
abc world
abc hello
containsKey---> false
str1 containsKey---> true
str2 containsKey---> true
value----> null
str1 value----> hello
str2 value----> world
我们可以知道IdentityHashMap是靠对象来判断key是否相等的,如果我们一个key需要保存多个value的时候就需要使用到这个IdentityHashMap类,这样我们我们就可以需要的时候使用到这个类了。
-
hashmap 允许key重复吗_关于HashMap集合 Key值相同时被覆盖问题( java中key值可以重复的map:...
2020-12-20 12:29:20那么问题来了,有时候后台需要我们发送json字符串数组,这时候就需要map集合的key值时相同的,网上找了很多方法,都是说重写key的hashCode()和map的put()方法,就可以实现对于相同key下多个value的存储.而我推荐的方法是...关于map集合,key值相同时,value值会被覆盖,查阅API相关的说明.
如果原来的key已经存在,则直接将新值覆盖到原值上面.
那么问题来了,有时候后台需要我们发送json字符串数组,这时候就需要map集合的key值时相同的,网上找了很多方法,
都是说重写key的hashCode()和map的put()方法,就可以实现对于相同key下多个value的存储.
而我推荐的方法是使用IdentityHashMap.
IdentityHashMap和HashMap之间的区别,根据网友的回复:
穿同样颜色衣服的双胞胎(HashMap)
穿不同颜色双胞胎弟弟(IdentityHashMap)
区别与其他的键不能重复的容器,IdentityHashMap允许key值重复,但是——key必须是两个不同的对象,即对于k1和k2,当k1==k2时,IdentityHashMap认为两个key相等,而HashMap只有在k1.equals(k2) == true 时才会认为两个key相等。
因此:IdentityHashMap maps = new IdentityHashMap<>();
maps.put("key",value1);
maps.put("key",
value2);
maps.put("key",
value3);
map.size = 3;
特此记录.
-
java map 允许重复_Java中的Map允许有重复元素吗?
2021-02-28 06:07:47Java中常见的三个集合接口:List、Set、Map,已经知道List中是允许有重复元素的,而Set中是不允许有重复元素的,那么Map中允许有重复元素吗?查阅资料,发现是不可以的,因为map是无序的,它的查询需要通过key的值来... -
hashmap 允许key重复吗_HashMap中key重复处理
2020-12-30 06:59:59用jdk本身的map是实现不了这种情况: 有重复key 但是不需要新的映射值value覆盖先前的value。public interface Map将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射一个值。自然继承一下HashMap,... -
java map 允许重复_Java中的Map允许有重复元素吗?|chu
2021-02-13 00:07:43Java中常见的三个集合接口:List、Set、Map,已经知道List中是允许有重复元素的,而Set中是不允许有重复元素的,那么Map中允许有重复元素吗?查阅资料,发现是不可以的,因为map是无序的,它的查询需要通过key的值来... -
key可以重复的Map集合:IdentityHashMap
2013-11-26 16:18:1813.7.5 key可以重复的Map集合:IdentityHashMap 之前所讲解的所有Map操作中key的值是不能重复的,例如,HashMap操作时key是不能重复的,如果重复则肯定会覆盖之前的内容,如下代码所示。 范例:Map中的key不允许... -
java map 允许重复_java,_HashMap的key值不允许重复问题,java - phpStudy
2021-02-28 06:06:39HashMap的key值不允许重复问题package com.wang.testMianShi;public class Person {private String name;private int age;public Person() {super();}public Person(String name, int age) {super();this.name = ... -
Java中key可以重复的Map集合:IdentityHashMap
2015-10-09 15:01:16范例:Map中的key不允许重复,重复就是覆盖 [java] view plaincopy package org.lxh.demo13.mapdemo; import java.util.HashMap; import java.util.Iterator; import... -
Java中的Map允许有重复元素吗?
2018-06-22 14:37:05Java中常见的三个集合接口:List、Set、Map,已经知道List中是允许有重复元素的,而Set中是不允许有重复元素的,那么Map中允许有重复元素吗?查阅资料,发现是不可以的,因为map是无序的,它的查询需要通过key的值来... -
java中key值可以重复的map:IdentityHashMap
2016-02-16 16:35:49在使用map的时候,大家肯定会想到key-value,key用于检索value的内容。在正常情况下,可以不允许重复;但是其实重复在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等,而IdentityHashMap用于后... -
Java 基础学习之类集框架 九 (key 可以重复的 Map 集合:IdentityHashMap)
2019-08-29 10:39:55之前讲解的所有 Map 操作中 key 的值是不能重复的,例如,HashMap 操作的时候是不能重复的,如果重复肯定会覆盖之前的内容。 实例 1 代码: package self.learn.setdemo; import java.util.HashMap; import ... -
hashmap 允许key重复吗_HashMap和HashTable详解
2020-12-11 22:33:34Map就是用来存储“键(key)-值(value) 对”的。 Map类中存储的“键值对”通过键来标识,所以“键对象”不能重复。Map 接口的实现类有HashMap、TreeMap、HashTable、Properties等。Map接口中常用的方法:HashMap采用... -
hashmap 允许key重复吗_你真的懂 HashMap 吗?
2021-01-02 07:17:58在介绍 HashMap 首先介绍下 Map 接口此接口位于 java.util 包下,该接口共有四个常用实现类,分别是 HashMap、LinkedHashMap、TreeMap、Hashtable。继承关系如图:HashMap 它... HashMap最多只允许一条记录的键为nul... -
hashmap 允许key重复吗_Java入职面试宝典HashMap的秘密
2020-12-27 13:36:36Java入职面试宝典--HashMap的秘密导语 HashMap是Java开发中...1概述HashMap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快的访问速度。遍历时,取得数据的顺序是完全随... -
hashmap 允许key重复吗_轻松理解HashMap
2020-12-23 08:22:52HashMap概述基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)此类不保证映射的顺序,特别是它... -
stl中map的key可以重复吗?
2020-07-03 18:05:56如果需要key值相同那么可以采用multimap,是允许key值重复的。 例如: m.insert(make_pair<int, int>(1, 3)); m.insert(make_pair<int, int>(0, 4)); m.insert(make_pair<int, int>(0, 2)); MyMap... -
hashmap 允许key重复吗_HashMap与Hashtable区别以及底层实现
2021-01-02 07:17:51在讲HashMap与Hashtable的区别前,先讲一讲Map接口... Map就是用来存储“键(key)-值(value) 对”的。 Map类中存储的“键值对”通过键来标识,所以“键对象”不能重复。 Map 接口的实现类有HashMap、TreeMap、HashTa... -
【小家java】Java中IdentityHashMap使用详解---允许key重复(阐述和HashMap的区别)
2018-08-21 02:52:58应该有很多人不知道IdentityHashMap的存在,其中不乏工作很多年的Java开发者,会有很多人以为这是第...其实我们对Map都有一个通用认知:只要key相同,就不能重复往里面put,但是你真的了解“相同”这两个字吗?看下... -
C++ STL中允许重复key的multimap
2016-10-30 22:09:00在实际的项目中可能会碰到key重复的情况,正常的MAP类型是不允许重复的key,所以就要使用multimap了,multimap的使用和map基本类似,可以无缝对接 #include <map> typedef pair<string, int... -
hashmap 允许key重复吗_关于Java IdentityHashMap的问题 为什么相同的key 第二次插入进去了?...
2021-01-12 14:00:42不废话,直接上IdentityHashMap中的put方法源码:/*** Associates the specified value with the specified key in this identity* hash map. If the map previously contained a mapping for the key, the* ... -
hashmap 允许key重复吗_深入理解HashSet(底层是HashMap)
2020-12-31 22:55:45hashSet集合是Collection的子类呀,其叔叔才是Map啊冷静分析一下,Set不能有重复的元素,HashMap不能有重复的键。HashSet概述和实现HashSet实现了Set接口,由哈希表(实际上是一个HashMap的实例)支持。它不保证Set的...