a collection of java (c) Set a collection of LinkedHashSet Detailed

problem

The bottom (1) LinkedHashSet of what storage element?

(2) LinkedHashSet and HashSet What is the difference?

(3) LinkedHashSet is ordered it?

(4) LinkedHashSet supported by elements of the access order to sort it?

Brief introduction

On a HashSet we say the elements are unordered, then there is no way to guarantee Set of elements is ordered it?

The answer is of course.

Today's hero LinkedHashSet have this feature, it is how orderly it? Let's learn together.

Source code analysis

LinkedHashSet inherits from HashSet, let's take a look at the source code directly on them any different.

Classes in java.util Package; 

// a LinkedHashSet Inherited from HashSet 
public  class a LinkedHashSet <E> 
    the extends HashSet <E> 
    the implements the Set <E> , the Cloneable, the java.io.Serializable { 

    Private  static Final Long serialVersionUID = - 2851667679971038690L ; 

    // incoming capacity and a load factor of 
    public a LinkedHashSet ( int initialCapacity, a float loadFactor) { 
        Super (initialCapacity, loadFactor, to true ); 
    } 
    
    // only incoming capacity, load factor defaults to 0.75 
    public a LinkedHashSet ( intinitialCapacity) { 
        Super (initialCapacity, .75f, to true ); 
    } 
    
    // the default capacity of 16, a default load factor of 0.75 
    public a LinkedHashSet () { 
        Super ( 16 , .75f, to true ); 
    } 

    // all the elements of the set of c LinkedHashSet added to the
     // strange, where computing capacity has changed the way
     // HashSet is used Math.max ((int) (c.size () /. 75f) +. 1, 16)
     // this a little hard to understand, is the author of lazy? 
    public a LinkedHashSet (Collection <the extends E?> C) { 
        Super (Math.max ( 2 * c.size (), . 11 ), .75f, to true );  
        the addAll (C);
    } 
    
    //Dividable iterator, primarily for use in parallel iterative process multithreaded 
    @Override
     public Spliterator <E> spliterator () {
         return Spliterators.spliterator ( the this , Spliterator.DISTINCT | Spliterator.ORDERED); 
    } 
}

Over, over, so much, this is all the source code, really.

Can be seen, a LinkedHashSet provides a total of five methods, four of which are construction method, there is an iterator.

4 constructor is called the father of super(initialCapacity, loadFactor, true);this method.

This method look like it?

Remember when we said that is not on a public constructor do? that's it.

    // constructor of HashSet 
    HashSet ( int initialCapacity, a float loadFactor, Boolean dummy) { 
        Map = new new a LinkedHashMap <> (initialCapacity, loadFactor); 
    }

As indicated above, the method which uses LinkedHashMap configured to initialize a HashSet map.

Now this logic should be very clear, LinkedHashSet inherits from HashSet, it's add, delete, query and other methods are used directly HashSet, the only difference is that it uses LinkedHashMap storage elements.

Well, it begins with a few questions if you can answer it?

to sum up

Bottom (1) LinkedHashSet LinkedHashMap storage element is used.

(2) LinkedHashSet are ordered, it is sorted in the order of insertion.

Egg

Through the above study, we know that the use of the underlying LinkedHashSet LinkedHashMap storage element, and is supported by elements LinkedHashMap access to traverse the elements of the order, that is, can be used to implement the LRU, remember? Portal [ Sike LinkedHashMap collection of java source code analysis ]

So, LinkedHashSet supported by elements of the access order to sort it?

Let's analyze the next.

First, LinkedHashSet all the construction methods are invoked with a HashSet constructor, as follows:

    // constructor of HashSet 
    HashSet ( int initialCapacity, a float loadFactor, Boolean dummy) { 
        Map = new new a LinkedHashMap <> (initialCapacity, loadFactor); 
    }

Then, by calling the constructor initialization LinkedHashMap map, as shown below:

    public LinkedHashMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
        accessOrder = false;
    }

It can be seen here accessOrder write to the dead is false.

So, LinkedHashSet is not supported by the access order to sort the elements can only be sorted by insertion order

Guess you like

Origin www.cnblogs.com/yuexiaoyun/p/12079366.html