Java collections examples - Map List Set

本文深入探讨了LinkedHashSet的数据结构,解释了其如何保持元素插入顺序,与HashSet和TreeSet的区别,以及在实际应用中的优势。通过代码示例展示了不同集合类在打印时的行为差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LinkedHashSet keeps the objects in the order in which they were added.

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.)

This implementation spares its clients from the unspecified, generally chaotic ordering provided by HashSet, without incurring the increased cost associated with TreeSet. It can be used to produce a copy of a set that has the same order as the original, regardless of the original set's implementation:

     void foo(Set s) {
         Set copy = new LinkedHashSet(s);
         // ...
     }

This technique is particularly useful if a module takes a set on input, copies it, and later returns results whose order is determined by that of the copy. (Clients generally appreciate having things returned in the same order they were presented.)

printing collections:

// collections/PrintingCollections.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Collections print themselves automatically

import java.util.*;

public class PrintingCollections {
  static Collection fill(Collection<String> collection) {
    collection.add("rat");
    collection.add("cat");
    collection.add("dog");
    collection.add("dog");
    return collection;
  }

  static Map fill(Map<String, String> map) {
    map.put("rat", "Fuzzy");
    map.put("cat", "Rags");
    map.put("dog", "Bosco");
    map.put("dog", "Spot");
    return map;
  }

  public static void main(String[] args) {
    System.out.println(fill(new ArrayList<>())); // [1]
    System.out.println(fill(new LinkedList<>())); // [1]
    System.out.println(fill(new HashSet<>())); // [1]
    System.out.println(fill(new TreeSet<>())); // [1]
    System.out.println(fill(new LinkedHashSet<>())); // [1]
    System.out.println(fill(new HashMap<>())); // [2]
    System.out.println(fill(new TreeMap<>())); // [2]
    System.out.println(fill(new LinkedHashMap<>())); // [2]
  }
}
/* Output:
[rat, cat, dog, dog]
[rat, cat, dog, dog]
[rat, cat, dog]
[cat, dog, rat]
[rat, cat, dog]
{rat=Fuzzy, cat=Rags, dog=Spot}
{cat=Rags, dog=Spot, rat=Fuzzy}
{rat=Fuzzy, cat=Rags, dog=Spot}
*/

[1] toString is overrided in AbstractCollection class.

[2] toString is overrided in AbstractMap class.

references:

1. On Java 8 - Bruce Eckel

2. https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashSet.html

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/PrintingCollections.java

4. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/AbstractList.java 

5. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/AbstractMap.java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值