作者:davidwzw2009413 | 来源:互联网 | 2023-05-18 21:05
学习目标:了解IdentityHashMap类的作用。掌握SortedMap接口的作用。在正常的Map操作中,key本身是不能够重复的。importjava.util.IdentityHashMap
学习目标:
了解IdentityHashMap类的作用。
掌握SortedMap接口的作用。
在正常的Map操作中,key本身是不能够重复的。
import java.util.IdentityHashMap ;
import java.util.HashMap ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Map ;
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 map = null ;// 声明Map对象
map = new HashMap() ;
map.put(new Person("张三",30),"zhangsan_1") ;// 加入内容
map.put(new Person("张三",30),"zhangsan_2") ;// 加入内容
map.put(new Person("李四",31),"lisi") ;// 加入内容
Set> allSet = null ;// 准备使用Set接收全部内容
allSet = map.entrySet() ;
Iterator> iter = null ;
iter = allSet.iterator() ;
while(iter.hasNext()){
Map.Entry me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};
使用HashMap操作的时候,key内容是不能重复的,如果现在希望key内容可以重复(指的是两个对象的地址不一样key1==key2 )则要使用IdentityHashMap类。
import java.util.IdentityHashMap ;
import java.util.Set ;
import java.util.Iterator ;
import java.util.Map ;
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 IdentityHashMapDemo02{
public static void main(String args[]){
Map map = null ;// 声明Map对象
map = new IdentityHashMap() ;
map.put(new Person("张三",30),"zhangsan_1") ;// 加入内容
map.put(new Person("张三",30),"zhangsan_2") ;// 加入内容
map.put(new Person("李四",31),"lisi") ;// 加入内容
Set> allSet = null ;// 准备使用Set接收全部内容
allSet = map.entrySet() ;
Iterator> iter = null ;
iter = allSet.iterator() ;
while(iter.hasNext()){
Map.Entry me = iter.next() ;
System.out.println(me.getKey() + " --> " + me.getValue()) ;
}
}
};
就算是两个对象的内容相等,但是因为都使用了new关键字,所以地址肯定不同,那么就可以添加进去,对于IdentityHashMap而言,不管覆写没有方法,key的内容都是可以相等的,因为它比较的是内存的地址。
SortedMap接口
SortedMap接口扩展的方法:
1、public Comparator super K> comparator()普通 返回比较器对象。
2、public K firstKey()普通 返回第一个元素的key。
3、public SortedMap headMap(K toKey) 普通 返回小于或等于指定key的部分集合。
4、public K lastKey() 普通 返回最后一个元素的key。
5、public SortedMap subMap(K fromKey, K toKey)普通 返回指定key范围的集合。
6、public SortedMap tailMap(K fromKey)返回大于指定key范围的集合。
import java.util.Map ;
import java.util.SortedMap ;
import java.util.TreeMap ;
public class SortedMapDemo{
public static void main(String args[]){
SortedMap map = null ;
map = new TreeMap() ;// 通过子类实例化接口对象
map.put("D","liuxun") ;
map.put("A","QQ") ;
map.put("C","webChat") ;
map.put("B","P2P") ;
System.out.print("第一个元素的内容的key:" + map.firstKey()) ;
System.out.println(":对应的值:" + map.get(map.firstKey())) ;
System.out.print("最后一个元素的内容的key:" + map.lastKey()) ;
System.out.println(":对应的值:" + map.get(map.lastKey())) ;
System.out.println("返回小于指定范围的集合:") ;
for(Map.Entry me:map.headMap("B").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
System.out.println("返回大于指定范围的集合:") ;
for(Map.Entry me:map.tailMap("B").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
System.out.println("部分集合:") ;
for(Map.Entry me:map.subMap("A","C").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
}
};
总结
1、对于IdentityHashMap而言,在实际开发中使用的不多。
2、SortedMap是Map接口的子接口。
3、在此接口中有很多的操作方法。
4、在实际中还是以Map接口为操作的标准。