Java collections examples - Map List Set

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/86181751

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

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/86181751