Java集合概述(三):Map集合

6 篇文章 0 订阅

Map集合概述

java集合最后一站之Map,给自己的总结画个句号。。。

Map用于保存具有映射关系的数据。

1.HashMap和Hashtable实现类

HashMap和Hashtable都是Map接口的典型实现类,它们之间的关系完全类似于Arraylist和Vecctor的关系。

区别:

Hashtable是线程安全的,HashMap是线程不安全的,所以HashMap比Hashtable的性能高一点。

Hashtable不允许使用null作为key和value;但HashMap可以使用null作为key或value。

HashMap和Hashtable判断两个value相等的标准:只要两个对象通过equals方法比较返回true即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import  java.util.*;
 
class  A
{
     int  count;
     public  A( int  count)
     {
         this .count = count;
     }
     public  boolean  equals(Object obj)
     {
         if  (obj ==  this )
         {
             return  true ;
         }
         if  (obj !=  null  &&
             obj.getClass() == A. class )
         {
             A a = (A)obj;
             if  ( this .count == a.count)
             {
                 return  true ;
             }
         }
         return  false ;
     }
     public  int  hashCode()
     {
         return  this .count;
     }
}
class  B
{
     private  String name;
     
     public  B(String name) {
         super ();
         this .name = name;
     }
 
     public  B() {
         super ();
     }
 
     public  boolean  equals(Object obj)
     {
         return  true ;
     }
}
public  class  TestHashMap
{
     public  static  void  main(String[] args)
     {
         HashMap hm =  new  HashMap();
         hm.put( new  A( 60000 ) ,  "Struts2" );
         hm.put( new  A( 87563 ) ,  "J2EE" );
         hm.put( new  A( 1232 ) ,  "Xiao" );
         System.out.println(hm); //{A@ea60=Struts2, A@4d0=Xiao, A@1560b=J2EE}
         
         //只要两个对象通过equals比较返回true,Hashtable就认为它们是相等的value。
         //因为B对象equals总是返回true,所以下面是true
         System.out.println(hm.containsValue( new  B( "测试" ))); //true
         //只要两个A对象的count属性相等,它们通过equals比较返回true,且hashCode相等
         //Hashtable即认为它们是相同的key,所以下面输出true。
         System.out.println(hm.containsKey( new  A( 87563 ))); //true
         //下面语句可以删除最后一个key-value对
         hm.remove( new  A( 1232 ));
         for  (Object key : hm.keySet())
         {
             System.out.print(key +  "---->" );
             System.out.print(hm.get(key) +  "\n" );
         }
     }
}

  输出结果:

{A@ea60=Struts2, A@4d0=Xiao, A@1560b=J2EE}
true
true
A@ea60---->Struts2
A@1560b---->J2EE

2.LinkedHashMap实现类

LinkedHashMap实现类使用链表来维护key-value的次序,可以记住键值对的插入顺序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import  java.util.*;
 
public  class  TestLinkedHashMap
{
     public  static  void  main(String[] args)
     {
         LinkedHashMap scores =  new  LinkedHashMap();
         scores.put( "语文"  80 );
         scores.put( "数学"  76 );
         scores.put( "英文"  76 );
         //遍历scores里的所有的key-value对
         for  (Object key : scores.keySet())
         {
             System.out.print(key +  "------>" );
             System.out.print(scores.get(key) +  "\n" );
         }
     }
}

  输出结果:

语文------>80
数学------>76
英文------>76

3.SoetedMap接口和TreeMap实现类

TreeMap存储key-value键值对时,需要根据key对节点进行排序。TreeMap可以保证所有的key-value对处于有序状态。也有两种排序方式:

  1)  自然排序:TreeMap的所有key必须实现Comparable接口,而且所有的key应该是同一个类的对象,否则抛出ClassCastException异常。

  2)  定制排序:创建TreeMap时,传入一个Comparator对象,该对象负责对TreeMap中的所有key进行排序。不需要Map的key实现Comparable接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import  java.util.*;
 
//R类,重写了equals方法,如果count属性相等返回true
//重写了compareTo(Object obj)方法,如果count属性相等返回0;
class  implements  Comparable
{
     int  count;
     public  R( int  count)
     {
         this .count = count;
     }
     public  String toString()
     {
         return  "R(count属性:"  + count +  ")" ;
     }
     public  boolean  equals(Object obj)
     {
         if  ( this  == obj)
         {
             return  true ;
         }
         if  (obj !=  null
             && obj.getClass() == R. class )
         {
             R r = (R)obj;
             if  (r.count ==  this .count)
             {
                 return  true ;
             }
         }
         return  false ;
     }
     public  int  compareTo(Object obj)
     {
         R r = (R)obj;
         if  ( this .count > r.count)
         {
             return  1 ;
         }
         else  if  ( this .count == r.count)
         {
             return  0 ;
         }
         else
         {
             return  - 1 ;
         }
     }
}
public  class  TestTreeMap
{
     public  static  void  main(String[] args)
     {
         TreeMap tm =  new  TreeMap();
         tm.put( new  R( 3 ) ,  "J2EE" );
         tm.put( new  R(- 5 ) ,  "Struts2" );
         tm.put( new  R( 9 ) ,  "ROR" );
         System.out.println(tm); //{R(count属性:-5)=Struts2, R(count属性:3)=J2EE, R(count属性:9)=ROR}
         //返回该TreeMap的第一个Entry对象
         System.out.println(tm.firstEntry());
         //返回该TreeMap的最后一个key值
         System.out.println(tm.lastKey());
         //返回该TreeMap的比new R(2)大的最小key值。
         System.out.println(tm.higherKey( new  R( 2 )));
         //返回该TreeMap的比new R(2)小的最大的key-value对。
         System.out.println(tm.lowerEntry( new  R( 2 )));
         //返回该TreeMap的子TreeMap
         System.out.println(tm.subMap( new  R(- 1 ) ,  new  R( 4 )));
 
     }
}

  输出结果:

{R(count属性:-5)=Struts2, R(count属性:3)=J2EE, R(count属性:9)=ROR}
R(count属性:-5)=Struts2
R(count属性:9)
R(count属性:3)
R(count属性:-5)=Struts2
{R(count属性:3)=J2EE}

4.各Map实现类的性能分析:

1. HashMap与Hashtable的效率大体相同,它们的实现机制几乎一样,HashMap线程不安全,Hashtable线程安全,所以HashMap快一点。

2. TreeMap中所有的key-value对处于有序状态,所以TreeMap比HashMap,Hashtable要慢(尤其是插入、删除),因为TreeMap底层采用红黑树来管理key-value对。

3. LinkedHashMap使用链表维护键值对,所以比HahMap慢一点。

对于一般的·应用场景,推荐使用HashMap。

 

转发出处:http://www.cnblogs.com/jycboy/p/javamap.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值