Java中HashMap与HashTable之间的区别

原文地址:http://quiz.geeksforgeeks.org/differences-between-hashmap-and-hashtable-in-java/

HashMap and Hashtable store key/value pairs in a hash table. When using a Hashtable or HashMap, we specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

HashMap与HashTable都是在一个哈希表中存储键值对。在用HashMap与HashTable的时候,我们指定一个对象作为key,指定一个值与这个key进行关联。然后呢这个key的值被哈希函数处理一下,得到的哈希编码就是刚刚那个值存在哈希表中的下标。

Sample Java code.

// A sample Java program to demonstrate HashMap and HashTable
import java.util.*;
import java.lang.*;
import java.io.*;
 
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main(String args[])
    {
        //----------hashtable -------------------------
        Hashtable<Integer,String> ht=new Hashtable<Integer,String>();
        ht.put(101," ajay");
        ht.put(101,"Vijay");
        ht.put(102,"Ravi");
        ht.put(103,"Rahul");
        System.out.println("-------------Hash table--------------");
        for (Map.Entry m:ht.entrySet()) {
            System.out.println(m.getKey()+" "+m.getValue());
        }
 
        //----------------hashmap--------------------------------
        HashMap<Integer,String> hm=new HashMap<Integer,String>();
        hm.put(100,"Amit");
        hm.put(104,"Amit");  // hash map allows duplicate values
        hm.put(101,"Vijay");
        hm.put(102,"Rahul");
        System.out.println("-----------Hash map-----------");
        for (Map.Entry m:hm.entrySet()) {
            System.out.println(m.getKey()+" "+m.getValue());
        }
    }
}

输出:

-------------Hash table--------------
103 Rahul
102 Ravi
101 Vijay
-----------Hash map-----------
100 Amit
101 Vijay
102 Rahul
104 Amit

Hashmap vs Hashtable
1. HashMap is non synchronized. It is not-thread safe and can’t be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads.

1. HashMap不是同步的。这玩意儿线程不安全,并且在没有合适的同步代码的时候在多个线程之间也不能被共享。然而HashTable是可以同步的,它是线程安全的并且多个线程之间可以共享。
2. HashMap allows one null key and multiple null values whereas Hashtable doesn’t allow any null key or value.

2.HashMap允许一个空键和多个空值,然而Hashtable不允许。
3. HashMap is generally preferred over HashTable if thread synchronization is not needed

3.如果线程同步不太要求的话,那么一般就用HashMap而不是HashTable。(译者注:现在貌似HashTable也不咋用了)

Why HashTable doesn’t allow null and HashMap does?

为啥HashTable不允许为空而HashMap允许?
To successfully store and retrieve objects from a HashTable, the objects used as keys must implement the hashCode method and the equals method. Since null is not an object, it can’t implement these methods. HashMap is an advanced version and improvement on the Hashtable. HashMap was created later.

为了从HashTable中能够成功地存储并检索出对象,那么用作key的对象必须得实现hashCode方法和equals方法。因为null不是对象,所以它不能实现这些方法。HashMap呢是个像比较HashTable而言是一种比较高级的版本,并且改进了许多,HashMap是后来才有的。

Sources:
http://docs.oracle.com/javase/6/docs/api/java/util/Hashtable.html:
http://qa.geeksforgeeks.org/558/differences-between-hashmap-hashtable-and-hashset-in-java?show=558#q558



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值