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构造函数

  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

Java中的LinkedHashSet相关推荐

  1. Java 中removelinked_Java LinkedHashSet remove()用法及代码示例

    Java.util.LinkedHashSet.remove(Object O)方法用于从LinkedHashSet中删除特定元素. 用法: LinkedHashSet.remove(Object O ...

  2. java中集合的模型特点_Java 学习笔记(十四)集合

    集合 在开发的时候,如果需要存储多个数据,可以使用数组,不过数据是固定长度的,如果需要存储可变长度的多个数据,可以使用集合.集合提供存储空间可变的存储类型,存储的容量可以实时改变 集合分类 Colle ...

  3. Java中的Set集合类

    1. 概述 Java 中的Set和正好和数学上直观的集(set)的概念是相同的.Set最大的特性就是不允许在其中存放的元素是重复的.根据这个特点,我们就可以使用Set 这个接口来实现前面提到的关于商品 ...

  4. Java Review - LinkedHashMap LinkedHashSet 源码解读

    文章目录 Pre 概述 数据结构 类继承关系 构造函数 方法 get() put() remove() LinkedHashSet 使用案例 - FIFO策略缓存 Pre Java Review - ...

  5. 浅谈Java中的Set、List、Map的区别

    就学习经验,浅谈Java中的Set,List,Map的区别,对JAVA的集合的理解是想对于数组: 数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型),JAVA集合可以存储和操 ...

  6. 第八节:详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架

    前言 大家好,给大家带来详细讲解Java中的异常处理情况与I/O流的介绍以及类集合框架的概述,希望你们喜欢 JAVA 异常 try...catch...finally结构的使用方法 class Tes ...

  7. java中的数据结构总结

    Java的类库实在是很多,以至于很多人都不太了解,结果总是自己造轮子. 下面汇总了Java中的一些数据结构,加上一些实现的分析,同时备忘. 至于时间复杂度,个人觉得写出来的用处不大.如果明白它是怎么实 ...

  8. 怎么在java中创建一个自定义的collector

    文章目录 简介 Collector介绍 自定义Collector 总结 怎么在java中创建一个自定义的collector 简介 在之前的java collectors文章里面,我们讲到了stream ...

  9. Java中List Set Map 是否有序等总结

    1.Collection List Set Map 区别记忆 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文 ...

最新文章

  1. Kafka Architecture
  2. python面向对象编程的优点-Python面向对象编程 一
  3. [转]文件浏览直接显示[兼容IE,FireFox]
  4. mysql 取一行_MySql – 如何获取上一行中的值和下一行中的值?
  5. SqlServer之代码块相关
  6. 【机房收费系统】多么痛的领悟
  7. 发一则自己创作的Lae程序员小漫画,仅供一乐
  8. linux band0 手动重启,band,call,apply的区别以及手动封装
  9. 读书笔记之101个脚本之No.8
  10. 12699元的iPhone 11 Pro Max物料成本却不足3500元,苹果赚疯了?
  11. android蜂巢效果、环形菜单、Kotlin影视应用、简约时钟、查看导出App、支付宝AR扫码效果等源码
  12. NOI Linux 2.0的安装
  13. C++编码实现定时任务执行功能
  14. Postgresql使用技巧
  15. 斑马Zebra LP2844 打印机驱动
  16. 睦星科技Kolmostar获1000万美元 A 轮融资,将推出更多元的GNSS定位解决方案
  17. Python3.x:第三方库简介
  18. <Android开发> Android vold - 第四篇 vold 的NetlinkHandler类简介
  19. 有意思的字符串查找函数strchr,strrchr,strstr,strrstr
  20. 把扫描文件转变为word文档的最实用的四款OCR识别软件

热门文章

  1. Tasker to stop Poweramp control for the headset while there is an incoming SMS - frozen
  2. [转载] python中numpy.concatenate()函数的使用
  3. [转载] python开源人工智能项目_推荐 10 个饱受好评且功能独特的开源人工智能项目
  4. docker入门与部署微服务--学习笔记
  5. Hystrix入门与分析(一):初识Hystrix
  6. 从数据类型 varchar 转换为 numeric 时出错.
  7. asp数据分页显示技术(上下页版)
  8. Win10的WSL很好用呀
  9. phoenix timestamp字段查询
  10. db link的查看创建与删除