我们平时使用的Map,都是只能在Map中保存一个相同的Key,我们后面保存的相同的key都会将原来的key的值覆盖掉,如下面的例子。
[java]
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"));
}
}
[java]
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
[java]
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