Java中Hashtable和HashMap类之间的区别

HashMap与HashSet (HashMap vs HashSet)

Here, we will see how Hashtable differs from HashMap in Java?

在这里,我们将看到Hashtable与Java中的HashMap有何不同?

哈希表类 (Hashtable class)

  • Hashtable is a class which is available in java.util package.

    Hashtable是一个可在java.util包中使用的类。

  • In Hashtable, every method of the class is synchronized so that it provides Thread Safety.

    在Hashtable中,该类的每个方法都是同步的,以便提供线程安全。

  • Hashtable is Thread Safe (i.e. only one thread is allowed to operate on Hashtable object at a time).

    Hashtable是线程安全的(即一次只允许一个线程在Hashtable对象上操作)。

  • In Hashtable one thread operates on Hashtable object at a time so it takes more time to complete the task or in other words, we can say it increases the waiting time of the thread.

    在Hashtable中,一个线程一次在Hashtable对象上操作,因此需要更多的时间来完成任务,换句话说,可以说它增加了线程的等待时间。

  • In the case of Hashtable, performance is low just because of taking the more waiting time of the thread.

    在Hashtable的情况下,性能低下只是因为花费了更多的线程等待时间。

  • We can insert null for both keys and values.

    我们可以为键和值插入null。

  • Hashtable is a legacy class because this class is introduced in earlier version 1.0 of Java so this class is the need to re-engineered to support collection when collection framework came.

    Hashtable是一个遗留类,因为该类是在Java的早期版本1.0中引入的,因此在收集框架到来时需要重新设计该类以支持收集。

  • Hashtable does not provide uniqueness fully (i.e. duplicate are not allowed for keys and duplicates are allowed for values).

    哈希表不能完全提供唯一性(即,键不允许重复,值不允许重复)。

  • In Hashtable insertion, the order is not preserved (i.e. insertion and retrieval order is not needed to be same).

    在哈希表插入中,不保留顺序(即,插入和检索顺序不需要相同)。

Example:

例:

import java.util.Hashtable;

class HashTableClass {
    int hashcode;

    // class constructor to instantiate hashcode
    HashTableClass(int hashcode) {
        this.hashcode = hashcode;
    }

    // override hashCode()
    public int hashCode() {
        return hashcode;
    }

    // override toString() for string conversion
    public String toString() {
        return hashcode + " ";
    }

    public static void main(String[] args) {
        // Creating an instance 
        Hashtable ht = new Hashtable();

        // By using put() to add few objects in Hashtable 
        ht.put(new HashTableClass(10), "Java");
        ht.put(new HashTableClass(3), "C");
        ht.put(new HashTableClass(4), "C++");
        ht.put(new HashTableClass(3), "Ruby");
        ht.put(new HashTableClass(5), "C");
        ht.put(new HashTableClass(6), "null");

        // Display Current Hashtable
        System.out.println("Current Hashtable is :" + ht);
    }
}

Output

输出量

E:\Programs>javac HashTableClass.java

E:\Programs>java HashTableClass
Current Hashtable is :{10 =Java, 6 =null, 5 =C, 4 =C++, 3 =Ruby, 3 =C}

Here, we will see how HashMap differs from Hashtable in Java?

在这里,我们将看到HashMap与Java中的Hashtable有何不同?

哈希图 (HashMap)

  • HashMap is a class which is available in java.util package.

    HashMap是一个可在java.util包中使用的类。

  • In HashMap no method of the class is synchronized so that it does not provide Thread Safety.

    在HashMap中,没有类的方法被同步,因此它不提供线程安全性。

  • HashMap is not Thread Safe (i.e. multiple threads is allowed to operate on Hashtable object at a time).

    HashMap不是线程安全的(即,一次允许多个线程对Hashtable对象进行操作)。

  • In HashMap multiple threads operate on Hashtable object at a time so it takes less time to complete the task or in other words, we can say it decrease the waiting time of the thread.

    在HashMap中,多个线程同时在Hashtable对象上运行,因此完成任务所需的时间更少,换句话说,我们可以说它减少了线程的等待时间。

  • In the case of HashMap performance is high just because of taking the less waiting time of the thread.

    在HashMap的情况下,性能高是因为花费了更少的线程等待时间。

  • We can insert null for both keys(once) and values(multiple times).

    我们可以为键(一次)和值(多次)插入null。

  • HashMap is not a legacy class because this class is introduced in later version 1.2 of Java so this class does not need to re-engineered to support collection when collection framework came.

    HashMap不是遗留类,因为该类是在Java的更高版本1.2中引入的,因此在收集框架到来时,无需重新设计该类来支持收集。

  • HashMap does not provide uniqueness fully (i.e. duplicate are not allowed for keys and duplicates are allowed for values).

    HashMap不能完全提供唯一性(即,键不允许重复,值不允许重复)。

  • In HashMap insertion order is not preserved (i.e. insertion and retrieval order is not needed to be same).

    在HashMap中,不保留插入顺序(即插入和检索顺序不需要相同)。

Example:

例:

// Java program to demonstrate the behavior of Map

import java.util.Collection;
import java.util.HashMap;

class HashMapClass {
    public static void main(String[] args) {
        // Creating an instance of HashMap
        HashMap hm = new HashMap();

        // By using put() method to add some values in HashMap
        hm.put("Java", 1000);
        hm.put("C", 2000);
        hm.put("C++", 3000);
        hm.put("Ruby", 4000);
        hm.put("Python", 1000);
        hm.put("null", null);
        hm.put("Django", null);

        // Here we will not get any error but one null is accepted for keys
        hm.put("null", 7000);

        // Display retrieval order of Map
        System.out.println("Current HashMap list is :" + hm);

        // by using values() to find values of HashMap
        Collection values = hm.values();

        // Display Values of HashMap
        System.out.println("Current HashMap Key values is :" + values);
    }
}

Output

输出量

E:\Programs>javac HashMapClass.java

E:\Programs>java HashMapClass
Current HashMap list is :{Ruby=4000, C=2000, Django=null, 
Python=1000, C++=3000, null=7000, Java=1000}
Current HashMap Key values is :[4000, 2000, null, 1000, 3000, 7000, 1000]


翻译自: https://www.includehelp.com/java/differences-between-hashtable-and-hashmap-class-in-java.aspx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值