HashMap和ConcurrentHashMap的区别

哈希图 是在传统集合下的类,而ConcurrentHashMap是在并发集合下的类,除此之外,它们之间还有各种不同之处:

null
  • HashMap本质上是非同步的,即HashMap不是线程安全的,而ConcurrentHashMap本质上是线程安全的。
  • HashMap性能相对较高,因为它本质上是非同步的,任何数量的线程都可以同时执行。但ConcurrentHashMap性能有时很低,因为有时线程需要等待ConcurrentHashMap。
  • 当一个线程迭代HashMap对象时,如果另一个线程尝试添加/修改对象的内容,那么我们将得到运行时异常消息 ConcurrentModificationException .而在ConcurrentHashMap中,我们在迭代时执行任何修改时都不会出现任何异常。

    使用HashMap

    // Java program to illustrate
    // HashMap drawbacks
    import java.util.HashMap;
    class HashMapDemo extends Thread
    {
    static HashMap<Integer,String> l= new HashMap<Integer,String>();
    public void run()
    {
    try
    {
    Thread.sleep( 1000 );
    // Child thread trying to add
    // new element in the object
    l.put( 103 , "D" );
    }
    catch (InterruptedException e)
    {
    System.out.println( "Child Thread going to add element" );
    }
    }
    public static void main(String[] args) throws InterruptedException
    {
    l.put( 100 , "A" );
    l.put( 101 , "B" );
    l.put( 102 , "C" );
    HashMapDemo t= new HashMapDemo();
    t.start();
    for (Object o : l.entrySet())
    {
    Object s=o;
    System.out.println(s);
    Thread.sleep( 1000 );
    }
    System.out.println(l);
    }
    }

    
    

    输出:

    100=A
    Exception in thread "main" java.util.ConcurrentModificationException
    

    使用ConcurrentHashMap

    // Java program to illustrate
    // HashMap drawbacks
    import java.util.HashMap;
    import java.util.concurrent.*;
    class HashMapDemo extends Thread
    {
    static ConcurrentHashMap<Integer,String> l =
    new ConcurrentHashMap<Integer,String>();
    public void run()
    {
    // Child add new element in the object
    l.put( 103 , "D" );
    try
    {
    Thread.sleep( 2000 );
    }
    catch (InterruptedException e)
    {
    System.out.println( "Child Thread going to add element" );
    }
    }
    public static void main(String[] args) throws InterruptedException
    {
    l.put( 100 , "A" );
    l.put( 101 , "B" );
    l.put( 102 , "C" );
    HashMapDemo t= new HashMapDemo();
    t.start();
    for (Object o : l.entrySet())
    {
    Object s=o;
    System.out.println(s);
    Thread.sleep( 1000 );
    }
    System.out.println(l);
    }
    }

    
    

    输出:

    100=A
    101=B
    102=C
    103=D
    {100=A, 101=B, 102=C, 103=D}
    
  • 在HashMap中,键和值允许为null值,而在ConcurrentHashMap中,键和值不允许为null值,否则会出现运行时异常 NullPointerException。

    使用HashMap

    //Java Program to illustrate ConcurrentHashMap behaviour
    import java.util.*;
    class ConcurrentHashMapDemo
    {
    public static void main(String[] args)
    {
    HashMap m= new HashMap();
    m.put( 100 , "Hello" );
    m.put( 101 , "Geeks" );
    m.put( 102 , "Geeks" );
    m.put( null , "World" );
    System.out.println(m);
    }
    }

    
    

    输出:

    {null=World, 100=Hello, 101=Geeks, 102=Geeks}
    

    使用ConcurrentHashMap

    //Java Program to illustrate HashMap behaviour
    import java.util.concurrent.*;
    class ConcurrentHashMapDemo
    {
    public static void main(String[] args)
    {
    ConcurrentHashMap m= new ConcurrentHashMap();
    m.put( 100 , "Hello" );
    m.put( 101 , "Geeks" );
    m.put( 102 , "Geeks" );
    m.put( null , "World" );
    System.out.println(m);
    }
    }

    
    

    输出:

    Exception in thread "main" java.lang.NullPointerException
    
  • HashMap在JDK 1.2中引入,而ConcurrentHashMap在JDK 1.5中由SUN Microsystem引入。
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享