【Java类集】_IdentityHashMap类笔记
2014-10-01 23:10
281 查看
【Java类集】_IdentityHashMap类笔记
分类: Java【Java类集】_IdentityHashMap类笔记
在正常的Map操作,key本身是不能够重复的。
[java] view
plaincopyprint?
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String toString(){
return "姓名:"+this.name+";年龄:"+this.age;
}
public boolean equals(Object obj){
if(this==obj){
return true;
}
if(!(obj instanceof Person)){
return false;
}
Person per = (Person) obj;
if(this.name.equals(per.name)&&this.age==per.age){
return true;
}else{
return false;
}
}
public int hashCode(){
return this.name.hashCode()*this.age;
}
}
public class HashMapDemo08{
public static void main(String args[]){
Map<Person,String> map = null;
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;
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
使用HashMap操作的时候,key 内容是不能重复的,如果现在希望key内容可以重复(指的重复是指两个对象的地址不一样key1==key2)则要使用IdentityHashMap类。
[java] view
plaincopyprint?
import java.util.Map;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Set;
class Person{
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String toString(){
return "姓名:"+this.name+";年龄:"+this.age;
}
public boolean equals(Object obj){
if(this==obj){
return true;
}
if(!(obj instanceof Person)){
return false;
}
Person per = (Person) obj;
if(this.name.equals(per.name)&&this.age==per.age){
return true;
}else{
return false;
}
}
public int hashCode(){
return this.name.hashCode()*this.age;
}
}
public class IdentityHashMapDemo01{
public static void main(String args[]){
Map<Person,String> map = null;
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;
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());
}
}
}
相关文章推荐
- 【Java类集】_IdentityHashMap类笔记
- 浪潮优派培训java笔记:第9章 类集Collections(容器)
- [笔记][Java 4 android] [035~044] 内部类、线程和类集框架
- 专题笔记--Java 类集框架
- 【Java类集】_认识类集、Collection接口笔记
- 14. JAVA 枚举(Enum、类集EnumMap&EnumSet 、实现接口、定义抽象方法) ----- 学习笔记
- 专题笔记--Java 类集框架
- 【Java类集】_Set接口笔记(实例亲测)
- 〖Java类集〗_范例讲解:多对多关系笔记
- 〖Java类集〗_范例讲解:一对多关系笔记
- 【Java类集】_LinkedList类笔记(实例亲测)
- 【Java类集】_List接口笔记(实例亲测)
- 【Java类集】_ListIterator接口笔记(实例亲测)
- 【Java类集】_Iterator接口笔记(实例亲测)
- JAVA笔记十(40-43 数组和类集框架)
- java笔记:第9章 类集Collections(容器)
- 【Java类集】_SortedSet接口笔记(实例亲测)
- 【Java类集】_Map接口笔记(实例亲测)
- 13. Java类集 Part 1(类集、Collection接口、List接口、Set接口、SortedSet接口、集合的输出) ----- 学习笔记
- 【Java类集】_排序及重复元素说明笔记(实例亲测)