Java中HashSet和LinkedHashSet之间的区别

HashSet和LinkedHashSet (HashSet and LinkedHashSet)

  • This class is available in java.util package.

    此类在java.util包中可用。

  • This is an implementation class of Set interface.

    这是Set接口的实现类。

  • HashSet class is the parent of the LinkedHashSet class.

    HashSet类是LinkedHashSet类的父级。

  • The underlying data structure to implement HashSet is Hashtable.

    实现HashSet的基础数据结构是Hashtable。

  • In HashSet insertion, the order is not preserved that means the insertion order of the elements is not needed to be the same as the retrieving order of the elements.

    在HashSet插入中,不保留顺序,这意味着元素的插入顺序不需要与元素的检索顺序相同。

  • This HashSet class is introduced in the earlier version of Java 1.2.

    此HashSet类在Java 1.2的早期版本中引入。

  • We should go for HashSet if the insertion order of the elements is not important.

    如果元素的插入顺序不重要,则应使用HashSet。

Example:

例:

Let suppose we have a HashSet with few elements. Here we are adding the elements in the order is [10,20,30,50,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.50,20,10,30].

假设我们有一个包含少量元素的HashSet。 在这里,我们以[10,20,30,50,null]的顺序添加元素,如果我们要检索元素,则检索元素的顺序可以不同(即,不需要相同的插入和检索)元素的顺序)。因此输出将不同,顺序将类似于[null.50,20,10,30]。

// Java program to demonstrate the behavior of HashSet

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 an elements into the HashSet
        hs.add(10);
        hs.add(20);
        hs.add(30);
        hs.add(50);
        hs.add(null);

        // Display HashSet elements
        System.out.println("Retrieval order of the elements in HashSet is :" + hs);
    }
}

Output

输出量

E:\Programs>javac HashSetClass.java

E:\Programs>java HashSetClass
Retrieval order of the elements in HashSet is :[null, 50, 20, 10, 30]

链接哈希集 (LinkedHashSet)

  • This class is available in java.util package.

    此类在java.util包中可用。

  • This is an implementation class of Set interface.

    这是Set接口的实现类。

  • LinkedHashSet class is the child of the HashSet class.

    LinkedHashSet类是HashSet类的子级。

  • The underlying data structure to implement LinkedHashSet is a combination of Hashtable and LinkedList.

    实现LinkedHashSet的基础数据结构是Hashtable和LinkedList的组合。

  • In LinkedHashSet insertion order is preserved that means the insertion order of the elements must be the same as the retrieving order of the elements.

    在LinkedHashSet中,保留插入顺序,这意味着元素的插入顺序必须与元素的检索顺序相同。

  • This LinkedHashSet class is introduced in the earlier version of Java 1.4.

    在Java 1.4的早期版本中引入了LinkedHashSet类。

  • We should go for LinkedHashSet if the insertion order of the elements is important.

    如果元素的插入顺序很重要,则应使用LinkedHashSet。

Example:

例:

Let suppose we have a LinkedHashSet with few elements. Here we are adding the elements in the order is [10,20,30,50,null] and if we are retrieving the elements so the order of retrieving elements must be the same (i.e. it must be the same insertion and retrieval order of the elements.) so the output will be the same and the order will be like [10,20,30,50,null].

假设我们有一个包含少量元素的LinkedHashSet。 在这里,我们以[10,20,30,50,null]的顺序添加元素,如果我们要检索元素,则检索元素的顺序必须相同(即,其插入和检索顺序必须相同)。元素。)因此输出将相同,并且顺序将类似于[10,20,30,50,null]。

// Java program to demonstrate the behavior of LinkedHashSet

import java.util.*;

class LinkedHashSetClass {
    public static void main(String[] args) {
        // Creating an instance of LinkedHashSet
        LinkedHashSet lhs = new LinkedHashSet();

        // By using add() method to add an elements into the LinkedHashSet
        lhs.add(10);
        lhs.add(20);
        lhs.add(30);
        lhs.add(50);
        lhs.add(null);

        // Display LinkedHashSet elements
        System.out.println("Retrieval order of the elements in LinkedHashSet is :" + lhs);
    }
}

Output

输出量

E:\Programs>javac LinkedHashSetClass.java

E:\Programs>java LinkedHashSetClass
Retrieval order of the elements in LinkedHashSet is :[10, 20, 30, 50, null]


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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值