java 中实现一个key保存多个value
2015-04-06 17:28
399 查看
转自:
/article/7710976.html
我们平时使用的HashMap,都是只能在Map中保存一个相同的Key,我们后面保存的相同的key都会将原来的key的值覆盖掉,如下面的例子。
[java]
view plaincopyprint?
package test62;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class test {
/**
* @param args
* @author 王新
*/
public static void main(String[] args) {
String str1 = new String("xx");
String str2 = new String("xx");
System.out.println(str1 == str2);
Map<String ,String> map = new HashMap<String,String>();
map.put(str1, "hello");
map.put(str2, "world");
for(Entry<String,String> entry :map.entrySet())
{
System.out.println(entry.getKey()+" " + entry.getValue());
}
System.out.println("---->" + map.get("xx"));
}
}
这个例子中我们可以看见相同的key只能保存一个value值,下面我们来看一种map可以实现一个key中保存多个value。这个map也就是IdentityHashMap。下面我们就来介绍下IdentityHashMap这个类的使用。
API上这样来解释这个类的:此类不是 通用 Map 实现!此类实现Map 接口时,它有意违反 Map 的常规协定,该协定在比较对象时强制使用equals 方法。此类设计仅用于其中需要引用相等性语义的罕见情况。
IdentityHashMap类利用哈希表实现 Map 接口,比较键(和值)时使用引用相等性代替对象相等性。我们来看看这个类的代码吧:
[java]
view plaincopyprint?
package test62;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Map.Entry;
public class test1 {
public static void main(String[] args) {
String str1 = "xx";
String str2 = "xx";
System.out.println(str1 == str2);
Map<String ,String> map = new IdentityHashMap<String ,String>();
map.put(str1, "hello");
map.put(str2, "world");
for(Entry<String,String> entry : map.entrySet())
{
System.out.println(entry.getKey()+" " + entry.getValue());
}
System.out.println("containsKey---> " + map.containsKey("xx"));
System.out.println("value----> " + map.get("xx"));
}
}
这端代码输出的结果如下:
true
xx world
containsKey---> true
value----> world
为什么我们的Key还是只保存了一个值????这个问题和《java解惑第62题一样》书上面是这样解释的,我们来看看:
语言规范保证了字符串是内存限定的,换句话说,相等的字符串常量同时也是相同的[JLS 15.28]。这可以确保在我们的程序中第二次出现的字符串字面常量“xx”引用到了与第一次相同的String实例上,因此尽管我们使用了一个IdentityHashMap来代替诸如HashMap这样的通用目的的Map实现,但是对程序的行为却不会产生任何影响。
我们来看看下面的代码就可以实现一个key保存两个value的情况。我们的代码如下:
[java]
view plaincopyprint?
package test62;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Map.Entry;
public class test1 {
public static void main(String[] args) {
String str1 = new String("xx");
String str2 = new String("xx");
System.out.println(str1 == str2);
Map<String ,String> map = new IdentityHashMap<String ,String>();
map.put(str1, "hello");
map.put(str2, "world");
for(Entry<String,String> entry : map.entrySet())
{
System.out.println(entry.getKey()+" " + entry.getValue());
}
System.out.println(" containsKey---> " + map.containsKey("xx"));
System.out.println("str1 containsKey---> " + map.containsKey(str1));
System.out.println("str2 containsKey---> " + map.containsKey(str2));
System.out.println(" value----> " + map.get("xx"));
System.out.println("str1 value----> " + map.get(str1));
System.out.println("str2 value----> " + map.get(str2));
}
}
我们的看看输出的结果为:
false
xx world
xx hello
containsKey---> false
str1 containsKey---> true
str2 containsKey---> true
value----> null
str1 value----> hello
str2 value----> world
我们可以知道IdentityHashMap是靠对象来判断key是否相等的,如果我们一个key需要保存多个value的时候就需要使用到这个IdentityHashMap类,这样我们我们就可以需要的时候使用到这个类了。
我相信平时的多积累总会为我们带来好处的。
/article/7710976.html
我们平时使用的HashMap,都是只能在Map中保存一个相同的Key,我们后面保存的相同的key都会将原来的key的值覆盖掉,如下面的例子。
[java]
view plaincopyprint?
package test62;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class test {
/**
* @param args
* @author 王新
*/
public static void main(String[] args) {
String str1 = new String("xx");
String str2 = new String("xx");
System.out.println(str1 == str2);
Map<String ,String> map = new HashMap<String,String>();
map.put(str1, "hello");
map.put(str2, "world");
for(Entry<String,String> entry :map.entrySet())
{
System.out.println(entry.getKey()+" " + entry.getValue());
}
System.out.println("---->" + map.get("xx"));
}
}
package test62; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class test { /** * @param args * @author 王新 */ public static void main(String[] args) { String str1 = new String("xx"); String str2 = new String("xx"); System.out.println(str1 == str2); Map<String ,String> map = new HashMap<String,String>(); map.put(str1, "hello"); map.put(str2, "world"); for(Entry<String,String> entry :map.entrySet()) { System.out.println(entry.getKey()+" " + entry.getValue()); } System.out.println("---->" + map.get("xx")); } }
这个例子中我们可以看见相同的key只能保存一个value值,下面我们来看一种map可以实现一个key中保存多个value。这个map也就是IdentityHashMap。下面我们就来介绍下IdentityHashMap这个类的使用。
API上这样来解释这个类的:此类不是 通用 Map 实现!此类实现Map 接口时,它有意违反 Map 的常规协定,该协定在比较对象时强制使用equals 方法。此类设计仅用于其中需要引用相等性语义的罕见情况。
IdentityHashMap类利用哈希表实现 Map 接口,比较键(和值)时使用引用相等性代替对象相等性。我们来看看这个类的代码吧:
[java]
view plaincopyprint?
package test62;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Map.Entry;
public class test1 {
public static void main(String[] args) {
String str1 = "xx";
String str2 = "xx";
System.out.println(str1 == str2);
Map<String ,String> map = new IdentityHashMap<String ,String>();
map.put(str1, "hello");
map.put(str2, "world");
for(Entry<String,String> entry : map.entrySet())
{
System.out.println(entry.getKey()+" " + entry.getValue());
}
System.out.println("containsKey---> " + map.containsKey("xx"));
System.out.println("value----> " + map.get("xx"));
}
}
这端代码输出的结果如下:
true
xx world
containsKey---> true
value----> world
package test62; import java.util.IdentityHashMap; import java.util.Map; import java.util.Map.Entry; public class test1 { public static void main(String[] args) { String str1 = "xx"; String str2 = "xx"; System.out.println(str1 == str2); Map<String ,String> map = new IdentityHashMap<String ,String>(); map.put(str1, "hello"); map.put(str2, "world"); for(Entry<String,String> entry : map.entrySet()) { System.out.println(entry.getKey()+" " + entry.getValue()); } System.out.println("containsKey---> " + map.containsKey("xx")); System.out.println("value----> " + map.get("xx")); } } 这端代码输出的结果如下: true xx world containsKey---> true value----> world
为什么我们的Key还是只保存了一个值????这个问题和《java解惑第62题一样》书上面是这样解释的,我们来看看:
语言规范保证了字符串是内存限定的,换句话说,相等的字符串常量同时也是相同的[JLS 15.28]。这可以确保在我们的程序中第二次出现的字符串字面常量“xx”引用到了与第一次相同的String实例上,因此尽管我们使用了一个IdentityHashMap来代替诸如HashMap这样的通用目的的Map实现,但是对程序的行为却不会产生任何影响。
我们来看看下面的代码就可以实现一个key保存两个value的情况。我们的代码如下:
[java]
view plaincopyprint?
package test62;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Map.Entry;
public class test1 {
public static void main(String[] args) {
String str1 = new String("xx");
String str2 = new String("xx");
System.out.println(str1 == str2);
Map<String ,String> map = new IdentityHashMap<String ,String>();
map.put(str1, "hello");
map.put(str2, "world");
for(Entry<String,String> entry : map.entrySet())
{
System.out.println(entry.getKey()+" " + entry.getValue());
}
System.out.println(" containsKey---> " + map.containsKey("xx"));
System.out.println("str1 containsKey---> " + map.containsKey(str1));
System.out.println("str2 containsKey---> " + map.containsKey(str2));
System.out.println(" value----> " + map.get("xx"));
System.out.println("str1 value----> " + map.get(str1));
System.out.println("str2 value----> " + map.get(str2));
}
}
我们的看看输出的结果为:
false
xx world
xx hello
containsKey---> false
str1 containsKey---> true
str2 containsKey---> true
value----> null
str1 value----> hello
str2 value----> world
package test62; import java.util.IdentityHashMap; import java.util.Map; import java.util.Map.Entry; public class test1 { public static void main(String[] args) { String str1 = new String("xx"); String str2 = new String("xx"); System.out.println(str1 == str2); Map<String ,String> map = new IdentityHashMap<String ,String>(); map.put(str1, "hello"); map.put(str2, "world"); for(Entry<String,String> entry : map.entrySet()) { System.out.println(entry.getKey()+" " + entry.getValue()); } System.out.println(" containsKey---> " + map.containsKey("xx")); System.out.println("str1 containsKey---> " + map.containsKey(str1)); System.out.println("str2 containsKey---> " + map.containsKey(str2)); System.out.println(" value----> " + map.get("xx")); System.out.println("str1 value----> " + map.get(str1)); System.out.println("str2 value----> " + map.get(str2)); } } 我们的看看输出的结果为: false xx world xx hello containsKey---> false str1 containsKey---> true str2 containsKey---> true value----> null str1 value----> hello str2 value----> world
我们可以知道IdentityHashMap是靠对象来判断key是否相等的,如果我们一个key需要保存多个value的时候就需要使用到这个IdentityHashMap类,这样我们我们就可以需要的时候使用到这个类了。
我相信平时的多积累总会为我们带来好处的。
相关文章推荐
- java Map 一个key其实可以保存多个value
- Key/Value存储引擎——Bitcask的Java实现
- 实现了一个简单的key-value存储系统
- java实现HTTP的post请求 key-value格式与json格式的区别
- java 实现将一个string保存到txt文档中
- 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key, 用个数作为value,放入到map集
- 使用Java Swing的JComboBox实现Html中Select的key-value功能
- Java中Map根据键值(key)或者值(value)进行排序实现
- Java中Map根据键值(key)或者值(value)进行排序实现
- Java实现过滤掉map集合中key或value为空的值示例
- 实现了一个简单的key-value存储系统
- MyBatis查询两个字段,返回Map,一个字段作为key,一个字段作为value的实现
- Java中Map根据键值(key)或者值(value)进行排序实现
- java工具类(比如一个key对应value)
- Java中如何实现Map的按值Value(非Key)排序
- JAVA HashMap单key多value的实现及迭代器打印(附源码)
- Java中Map根据键值(key)或者值(value)进行排序实现
- Java Map 按key排序和按Value排序的实现方法
- 递归解析任意层的Json数据,获取所有的key和value,Java实现
- 使用php实现一个简单的key-value数据库