HashSet vs LinkedHashSet

后端 未结 10 2496
暗喜
暗喜 2020-11-28 18:20

What is the difference between them? I know that

A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all el

相关标签:
10条回答
  • 2020-11-28 18:50

    If you take a look at the constructors called from the LinkedHashSet class you will see that internally it's a LinkedHashMap that is used for backing purpose.

    0 讨论(0)
  • 2020-11-28 18:51

    I suggest you to use LinkedHashSet most of the time, because it has better performance overall):

    1. Predictable iteration order LinkedHashSet (Oracle)
    2. LinkedHashSet is more expensive for insertions than HashSet;
    3. In general slightly better performance than HashMap, because the most of the time we use Set structures for iterating.

    Performance tests:

    ------------- TreeSet -------------
     size       add  contains   iterate
       10       746       173        89
      100       501       264        68
     1000       714       410        69
    10000      1975       552        69
    ------------- HashSet -------------
     size       add  contains   iterate
       10       308        91        94
      100       178        75        73
     1000       216       110        72
    10000       711       215       100
    ---------- LinkedHashSet ----------
     size       add  contains   iterate
       10       350        65        83
      100       270        74        55
     1000       303       111        54
    10000      1615       256        58
    

    You can see source test page here: The Final Performance Testing Example

    0 讨论(0)
  • 2020-11-28 18:52

    All Methods and constructors are same but only one difference is LinkedHashset will maintain insertion order but it will not allow duplicates.

    Hashset will not maintain any insertion order. It is combination of List and Set simple :)

    0 讨论(0)
  • 2020-11-28 18:53

    HashSet: Unordered actually. if u passing the parameter means

    Set<Integer> set=new HashSet<Integer>();
    for(int i=0;i<set.length;i++)
    {
      SOP(set)`enter code here`
    }
    

    Out Put: May be 2,1,3 not predictable. next time another order.

    LinkedHashSet() which produce FIFO Order.

    0 讨论(0)
  • 2020-11-28 18:54

    HashSet don't maintain the order of insertion item
    LinkedHashSet maintain the order of insertion item

    Example

    Set<String> set = ...;// using new HashSet<>() OR new LinkedHashSet<>()
    set.add("2");
    set.add("1");
    set.add("ab");
    for(String value : set){
       System.out.println(value);
    }  
    

    HashSet output

    1
    ab
    2
    

    LinkedHashSet output

    2
    1
    ab
    
    0 讨论(0)
  • 2020-11-28 18:55

    LinkedHashSet's constructors invoke the following base class constructor:

    HashSet(int initialCapacity, float loadFactor, boolean dummy) {
      map = new LinkedHashMap<E, Object>(initialCapacity, loadFactor);
    }
    

    As you can see, the internal map is a LinkedHashMap. If you look inside LinkedHashMap, you'll discover the following field:

    private transient Entry<K, V> header;
    

    This is the linked list in question.

    0 讨论(0)
提交回复
热议问题