Java中HashSet和HashMap类之间的区别

HashMap与HashSet (HashMap vs HashSet)

First, we will see how HashMap differs from HashSet in Java?

首先,我们将看到HashMap与Java中的HashSet有何不同?

哈希图 (HashMap)

  • This class is available in java.util package.

    此类在java.util包中可用。

  • This class is an implementation class of Map interface.

    此类是Map接口的实现类。

  • HashMap is a parent class of LinkedHashMap.

    HashMap是LinkedHashMap的父类。

  • The underlying data structure of HashMap is Hashtable.

    HashMap的基础数据结构是Hashtable。

  • In HashMap "insertion order is not preserved" because it is based on HashCode of keys(i.e. the insertion order is not needed to be same as the retrieval order).

    在HashMap中,“插入顺序不保留”,因为它基于键的HashCode(即,插入顺序不需要与检索顺序相同)。

  • In HashMap object is represented in the form of keys and values where "duplicate keys are not allowed" but the " duplicate values are allowed".

    在HashMap中,对象以键和值的形式表示,其中“不允许重复的键”但“允许重复的值”。

  • In HashMap null can be inserted for both keys and values but we can insert "null" once for keys and we can insert "null" multiple times for values.

    在HashMap中,键和值都可以插入null,但是键可以一次插入“ null”,而值可以多次插入“ null”。

  • In HashMap heterogenous object are allowed for both keys and values.

    在HashMap中,键和值均允许使用异构对象。

Example:

例:

Let suppose we have a HashMap with few elements. Here we are adding the elements in the order is {Java=1000, C=2000, C++=3000, Ruby=4000, Python=1000, null=null, Django=null} and if we are retrieving the elements so the order of retrieving elements can be different (i.e. it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like {Ruby=4000, C=2000, Django=null, Python=1000, C++=3000, null=null, Java=1000}

假设我们有一个包含少量元素的HashMap。 在这里,我们按{Java = 1000,C = 2000,C ++ = 3000,Ruby = 4000,Python = 1000,null = null,Django = null}的顺序添加元素,如果我们要检索元素,则顺序为检索元素可以是不同的(即不需要元素的插入和检索顺序相同。)因此输出将是不同的,并且顺序将类似于{Ruby = 4000,C = 2000,Django = null,Python = 1000,C ++ = 3000,null = null,Java = 1000}

// Java program to demonstrate the behavior of HashMap

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);
        // hm.put("null",null); Here we will not get any error 
        // but one null is accepted for keys

        // Display retrieval order of HashMap
        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=null, Java=1000}
Current HashMap Key values is :[4000, 2000, null, 1000, 3000, null, 1000].

Second, we will see how HashSet differs from HashMap in Java?

其次,我们将看到HashSet与Java中的HashMap有何不同?

哈希集 (HashSet)

  • This class is available in java.util package.

    此类在java.util包中可用。

  • This class is an implementation class of Set interface.

    此类是Set接口的实现类。

  • HashSet is a parent class of LinkedHashSet.

    HashSet是LinkedHashSet的父类。

  • The underlying data structure of HashSet is Hashtable.

    HashSet的基础数据结构是Hashtable。

  • In HashSet "insertion order is not preserved" (i.e. the insertion order is not needed to be same as the retrieval order).

    在HashSet中,“不保留插入顺序”(即,插入顺序不需要与检索顺序相同)。

  • In HashSet "duplicate values are not allowed".

    在HashSet中,“不允许重复值”。

  • In HashSet null can be inserted for values.

    在HashSet中,可以为值插入null。

  • In HashSet heterogenous object are allowed.

    在HashSet中,允许异类对象。

Example:

例:

Let suppose we have a HashSet with few elements. Here we are adding the elements in the order is [1000, 2000, 3000, 4000, null] and if we are retrieving the elements so the order of retrieving elements can be different (i.e. it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like [null, 1000, 2000, 3000, 4000]

假设我们有一个包含少量元素的HashSet。 在这里,我们以[1000,2000,3000,4000,null]的顺序添加元素,如果我们正在检索元素,则检索元素的顺序可以不同(即,不需要相同的插入和检索)元素的顺序。)因此输出将有所不同,并且顺序将类似于[null,1000、2000、3000、4000]

import java.util.*;

class HashSetClass {
    public static void main(String[] args) {
        // Creating an instance of HashSet
        HashSet hs = new HashSet();

        //By using add() method to add some values in HashSet
        hs.add(1000);
        hs.add(2000);
        hs.add(3000);
        hs.add(4000);
        hs.add(null);
        // hs.add(2000); here we will not get any error or exception 
        // it will be ignored

        // Display retrieval order of HashSet
        System.out.println("Current HashSet list is :" + hs);
    }
}

Output

输出量

E:\Programs>javac HashSetClass.java

E:\Programs>java HashSetClass
Current HashSet list is :[null, 1000, 2000, 3000, 4000]


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

你可能感兴趣的:(数据结构,java,python,mysql,队列)