javaee论坛

普通会员

225648

帖子

334

回复

348

积分

楼主
发表于 2017-06-09 06:16:50 | 查看: 813 | 回复: 0

学习目标:

了解IdentityHashMap类的作用。
掌握SortedMap接口的作用。

在正常的Map操作中,key本身是不能够重复的。
< code_snippet_id="1842924" snippet_file_name="blog_20160821_1_6764644" name="code" class="java">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类。
< code_snippet_id="1842924" snippet_file_name="blog_20160821_2_843845" name="code" class="java">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范围的集合。
< code_snippet_id="1842924" snippet_file_name="blog_20160821_3_2937396" name="code" class="java">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接口为操作的标准。


您需要登录后才可以回帖 登录 | 立即注册

触屏版| 电脑版

技术支持 历史网 V2.0 © 2016-2017