Java中的LinkedHashSet

1. LinkedHashSet类 (1. LinkedHashSet Class)

  • LinkedHashSet class is the hash table and linked list implementation of the Set interface, with predictable iteration order.

    LinkedHashSet类是Set接口的哈希表和链表实现,具有可预测的迭代顺序。
  • It maintains a doubly linked list to maintain the order of elements.

    它维护一个双向链表以维护元素的顺序。
  • LinkedHashSet iterator returns elements in the order they were inserted, so the elements are iterated in the insertion-order.

    LinkedHashSet迭代器按插入顺序返回元素,因此将按插入顺序对其进行迭代。
  • The iteration order is not affected if an element is re-inserted into the LinkedHashSet.

    如果将元素重新插入LinkedHashSet中,则迭代顺序不会受到影响。
  • It’s useful when we want a Set that maintains insertion-order for iteration, but doesn’t want to add the extra overhead associated with the TreeSet.

    当我们想要一个Set保持迭代的插入顺序,但又不想增加与TreeSet相关的额外开销时,这很有用。

2. LinkedHashSet构造函数 (2. LinkedHashSet Constructors)

There are four constructors to create LinkedHashSet instance.

有四个构造函数来创建LinkedHashSet实例。

LinkedHashSet Constructors

LinkedHashSet Constructors

LinkedHashSet构造函数

  1. LinkedHashSet(): creates an empty linked hash set with the default initial capacity (16) and load factor (0.75).

    LinkedHashSet() :使用默认的初始容量(16)和负载因子(0.75)创建一个空的链接哈希集。
  2. LinkedHashSet(int initialCapacity): creates a new, empty linked hash set with the specified initial capacity and the default load factor (0.75). It throws the IllegalArgumentException if the initial capacity is less than 0.

    LinkedHashSet(int initialCapacity) :创建一个具有指定初始容量和默认负载因子(0.75)的新的空链接哈希集。 如果初始容量小于0,则抛出IllegalArgumentException。
  3. LinkedHashSet(int initialCapacity, float loadFactor): creates an empty LinkedHashSet with the given initial capacity and load factor. It throws IllegalArgumentException if the initial capacity is less than zero, or if the load factor is nonpositive.

    LinkedHashSet(int initialCapacity,float loadFactor) :使用给定的初始容量和负载因子创建一个空的LinkedHashSet。 如果初始容量小于零,或者负载系数为正,则抛出IllegalArgumentException。
  4. LinkedHashSet(Collection<? extends E> c): creates a new linked hash set with the same elements as the specified collection. It throws NullPointerException if the given collection is null.

    LinkedHashSet(Collection <?扩展E> c) :创建一个新的链接的哈希集,其元素与指定的集合相同。 如果给定的集合为null,则抛出NullPointerException

3. LinkedHashSet迭代顺序示例 (3. LinkedHashSet Iteration Order Example)

A simple example to show that LinkedHashSet iterator returns elements in the order of insertion. The same is not true for the HashSet.

一个简单的示例显示LinkedHashSet迭代器按插入顺序返回元素。 对于HashSet,情况并非如此。

HashSet<String> hs = new HashSet<>();
hs.add("a");
hs.add("i");
hs.add("u");
hs.add("e");
hs.add("o");

System.out.println(hs);
hs.forEach(System.out::println);

LinkedHashSet<String> lhs = new LinkedHashSet<>();
lhs.add("a");
lhs.add("i");
lhs.add("u");
lhs.add("e");
lhs.add("o");

System.out.println(lhs);
lhs.forEach(System.out::println);

Output:

输出:

[a, u, e, i, o]
a
u
e
i
o
[a, i, u, e, o]
a
i
u
e
o

4. LinkedHashSet与HashSet (4. LinkedHashSet vs HashSet)

  • LinkedHashSet iterator return elements in the insertion order. HashSet iterator doesn’t maintain any order.

    LinkedHashSet迭代器按插入顺序返回元素。 HashSet迭代器不维护任何顺序。
  • LinkedHashSet performance is slightly below that of HashSet, due to the added expense of maintaining the linked list, with one exception: Iteration over a LinkedHashSet requires time proportional to the size of the set, regardless of its capacity. Iteration over a HashSet is likely to be more expensive, requiring time proportional to its capacity.

    LinkedHashSet的性能比HashSet的性能稍低,这是由于维护链表的开销增加的,但有一个例外:在LinkedHashSet上进行迭代需要的时间与集的大小成正比,而与集的大小无关。 在HashSet上进行迭代可能会更昂贵,需要的时间与其容量成正比。

5. LinkedHashSet与TreeSet (5. LinkedHashSet vs TreeSet)

If you are looking for a Set implementation where the elements are iterated in the order of insertion, use LinkedHashSet. It saves the extra performance cost associated with the TreeSet.

如果要查找Set实现,其中按插入顺序对元素进行迭代,请使用LinkedHashSet。 这样可以节省与TreeSet相关的额外性能成本。

Reference: LinkedHashSet API Docs

参考LinkedHashSet API文档

翻译自: https://www.journaldev.com/33448/linkedhashset-in-java

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值