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

£神魔★判官ぃ 2022-09-25 11:22 389阅读 0赞

学习目标:

了解IdentityHashMap类的作用。

掌握SortedMap接口的作用。

在正常的Map操作中,key本身是不能够重复的。

  1. import java.util.IdentityHashMap ;
  2. import java.util.HashMap ;
  3. import java.util.Set ;
  4. import java.util.Iterator ;
  5. import java.util.Map ;
  6. class Person{
  7. private String name ;
  8. private int age ;
  9. public Person(String name,int age){
  10. this.name = name ;
  11. this.age = age ;
  12. }
  13. public boolean equals(Object obj){
  14. if(this==obj){
  15. return true ;
  16. }
  17. if(!(obj instanceof Person)){
  18. return false ;
  19. }
  20. Person p = (Person)obj ;
  21. if(this.name.equals(p.name)&&this.age==p.age){
  22. return true ;
  23. }else{
  24. return false ;
  25. }
  26. }
  27. public int hashCode(){
  28. return this.name.hashCode() * this.age ;
  29. }
  30. public String toString(){
  31. return "姓名:" + this.name + ",年龄:" + this.age ;
  32. }
  33. };
  34. public class IdentityHashMapDemo01{
  35. public static void main(String args[]){
  36. Map<Person,String> map = null ; // 声明Map对象
  37. map = new HashMap<Person,String>() ;
  38. map.put(new Person("张三",30),"zhangsan_1") ; // 加入内容
  39. map.put(new Person("张三",30),"zhangsan_2") ; // 加入内容
  40. map.put(new Person("李四",31),"lisi") ; // 加入内容
  41. Set<Map.Entry<Person,String>> allSet = null ; // 准备使用Set接收全部内容
  42. allSet = map.entrySet() ;
  43. Iterator<Map.Entry<Person,String>> iter = null ;
  44. iter = allSet.iterator() ;
  45. while(iter.hasNext()){
  46. Map.Entry<Person,String> me = iter.next() ;
  47. System.out.println(me.getKey() + " --> " + me.getValue()) ;
  48. }
  49. }
  50. };

Center
使用HashMap操作的时候,key内容是不能重复的,如果现在希望key内容可以重复(指的是两个对象的地址不一样key1==key2 )则要使用IdentityHashMap类。

  1. import java.util.IdentityHashMap ;
  2. import java.util.Set ;
  3. import java.util.Iterator ;
  4. import java.util.Map ;
  5. class Person{
  6. private String name ;
  7. private int age ;
  8. public Person(String name,int age){
  9. this.name = name ;
  10. this.age = age ;
  11. }
  12. public boolean equals(Object obj){
  13. if(this==obj){
  14. return true ;
  15. }
  16. if(!(obj instanceof Person)){
  17. return false ;
  18. }
  19. Person p = (Person)obj ;
  20. if(this.name.equals(p.name)&&this.age==p.age){
  21. return true ;
  22. }else{
  23. return false ;
  24. }
  25. }
  26. public int hashCode(){
  27. return this.name.hashCode() * this.age ;
  28. }
  29. public String toString(){
  30. return "姓名:" + this.name + ",年龄:" + this.age ;
  31. }
  32. };
  33. public class IdentityHashMapDemo02{
  34. public static void main(String args[]){
  35. Map<Person,String> map = null ; // 声明Map对象
  36. map = new IdentityHashMap<Person,String>() ;
  37. map.put(new Person("张三",30),"zhangsan_1") ; // 加入内容
  38. map.put(new Person("张三",30),"zhangsan_2") ; // 加入内容
  39. map.put(new Person("李四",31),"lisi") ; // 加入内容
  40. Set<Map.Entry<Person,String>> allSet = null ; // 准备使用Set接收全部内容
  41. allSet = map.entrySet() ;
  42. Iterator<Map.Entry<Person,String>> iter = null ;
  43. iter = allSet.iterator() ;
  44. while(iter.hasNext()){
  45. Map.Entry<Person,String> me = iter.next() ;
  46. System.out.println(me.getKey() + " --> " + me.getValue()) ;
  47. }
  48. }
  49. };

Center 1
就算是两个对象的内容相等,但是因为都使用了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范围的集合。

  1. import java.util.Map ;
  2. import java.util.SortedMap ;
  3. import java.util.TreeMap ;
  4. public class SortedMapDemo{
  5. public static void main(String args[]){
  6. SortedMap<String,String> map = null ;
  7. map = new TreeMap<String,String>() ; // 通过子类实例化接口对象
  8. map.put("D","liuxun") ;
  9. map.put("A","QQ") ;
  10. map.put("C","webChat") ;
  11. map.put("B","P2P") ;
  12. System.out.print("第一个元素的内容的key:" + map.firstKey()) ;
  13. System.out.println(":对应的值:" + map.get(map.firstKey())) ;
  14. System.out.print("最后一个元素的内容的key:" + map.lastKey()) ;
  15. System.out.println(":对应的值:" + map.get(map.lastKey())) ;
  16. System.out.println("返回小于指定范围的集合:") ;
  17. for(Map.Entry<String,String> me:map.headMap("B").entrySet()){
  18. System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
  19. }
  20. System.out.println("返回大于指定范围的集合:") ;
  21. for(Map.Entry<String,String> me:map.tailMap("B").entrySet()){
  22. System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
  23. }
  24. System.out.println("部分集合:") ;
  25. for(Map.Entry<String,String> me:map.subMap("A","C").entrySet()){
  26. System.out.println("\t|- " + me.getKey() + " --> " + me.getValue()) ;
  27. }
  28. }
  29. };

Center 2

总结

1、对于IdentityHashMap而言,在实际开发中使用的不多。

2、SortedMap是Map接口的子接口。

3、在此接口中有很多的操作方法。

4、在实际中还是以Map接口为操作的标准。

发表评论

表情:
评论列表 (有 0 条评论,389人围观)

还没有评论,来说两句吧...

相关阅读