您的位置:首页 > 编程语言 > Java开发

Java类集框架——IdentityHashMap类以及SortedMap接口子类TreeMap的具体使用

2016-08-21 16:02 567 查看

学习目标:

了解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<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()) ;
}
}
};



使用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<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()) ;
}
}
};



就算是两个对象的内容相等,但是因为都使用了new关键字,所以地址肯定不同,那么就可以添加进去,对于IdentityHashMap而言,不管覆写没有方法,key的内容都是可以相等的,因为它比较的是内存的地址。

SortedMap接口

SortedMap接口扩展的方法:
1、public Comparator<? super K> comparator()
普通 返回比较器对象。
2、public K firstKey()
普通 返回第一个元素的key。
3、public SortedMap<K, V> headMap(K toKey)
 普通 返回小于或等于指定key的部分集合。
4、public K lastKey()  普通 返回最后一个元素的key。
5、public SortedMap<K, V> subMap(K fromKey, K  toKey)
普通 返回指定key范围的集合。
6、public SortedMap<K, V> 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<String,String> map = null ;
map = new TreeMap<String,String>() ;	// 通过子类实例化接口对象
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<String,String> me:map.headMap("B").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
System.out.println("返回大于指定范围的集合:") ;
for(Map.Entry<String,String> me:map.tailMap("B").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
System.out.println("部分集合:") ;
for(Map.Entry<String,String> me:map.subMap("A","C").entrySet()){
System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
}
}
};




总结
1、对于IdentityHashMap而言,在实际开发中使用的不多。
2、SortedMap是Map接口的子接口。
3、在此接口中有很多的操作方法。
4、在实际中还是以Map接口为操作的标准。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐