博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中的LinkedHashSet
阅读量:2534 次
发布时间:2019-05-11

本文共 3780 字,大约阅读时间需要 12 分钟。

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 if the given collection is null.

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

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
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
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]aueio[a, i, u, e, o]aiueo

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:

参考

翻译自:

转载地址:http://wplzd.baihongyu.com/

你可能感兴趣的文章
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>
RTMP
查看>>
求一个数的整数次方
查看>>
点云PCL中小细节
查看>>
铁路信号基础
查看>>
RobotFramework自动化2-自定义关键字
查看>>
[置顶] 【cocos2d-x入门实战】微信飞机大战之三:飞机要起飞了
查看>>