Home | History | Annotate | Download | only in util
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package java.util;
     19 
     20 import java.io.Serializable;
     21 
     22 /**
     23  * LinkedHashSet is a variant of HashSet. Its entries are kept in a
     24  * doubly-linked list. The iteration order is the order in which entries were
     25  * inserted.
     26  * <p>
     27  * Null elements are allowed, and all the optional Set operations are supported.
     28  * <p>
     29  * Like HashSet, LinkedHashSet is not thread safe, so access by multiple threads
     30  * must be synchronized by an external mechanism such as
     31  * {@link Collections#synchronizedSet(Set)}.
     32  *
     33  * @since 1.4
     34  */
     35 public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable,
     36         Serializable {
     37 
     38     private static final long serialVersionUID = -2851667679971038690L;
     39 
     40     /**
     41      * Constructs a new empty instance of {@code LinkedHashSet}.
     42      */
     43     public LinkedHashSet() {
     44         super(new LinkedHashMap<E, HashSet<E>>());
     45     }
     46 
     47     /**
     48      * Constructs a new instance of {@code LinkedHashSet} with the specified
     49      * capacity.
     50      *
     51      * @param capacity
     52      *            the initial capacity of this {@code LinkedHashSet}.
     53      */
     54     public LinkedHashSet(int capacity) {
     55         super(new LinkedHashMap<E, HashSet<E>>(capacity));
     56     }
     57 
     58     /**
     59      * Constructs a new instance of {@code LinkedHashSet} with the specified
     60      * capacity and load factor.
     61      *
     62      * @param capacity
     63      *            the initial capacity.
     64      * @param loadFactor
     65      *            the initial load factor.
     66      */
     67     public LinkedHashSet(int capacity, float loadFactor) {
     68         super(new LinkedHashMap<E, HashSet<E>>(capacity, loadFactor));
     69     }
     70 
     71     /**
     72      * Constructs a new instance of {@code LinkedHashSet} containing the unique
     73      * elements in the specified collection.
     74      *
     75      * @param collection
     76      *            the collection of elements to add.
     77      */
     78     public LinkedHashSet(Collection<? extends E> collection) {
     79         super(new LinkedHashMap<E, HashSet<E>>(collection.size() < 6 ? 11
     80                 : collection.size() * 2));
     81         for (E e : collection) {
     82             add(e);
     83         }
     84     }
     85 
     86     /* overrides method in HashMap */
     87     @Override
     88     HashMap<E, HashSet<E>> createBackingMap(int capacity, float loadFactor) {
     89         return new LinkedHashMap<E, HashSet<E>>(capacity, loadFactor);
     90     }
     91 }
     92