Java_LinkedHashSet工作原理

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

LinkedHashSet是基于HashSet实现的,在HashSet的基础上,维持了一个双向链表的关系。可以对比HashMap 与LinkedHashMap的链表维持。

LinkedHashSet:

//直接继承与HashSet,可序列化
public class LinkedHashSet<E>
    extends HashSet<E>
    implements Set<E>, Cloneable, java.io.Serializable {....}
  //相应的构造方法均是调用HashSet方法
 public LinkedHashSet(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor, true);
    }
 public LinkedHashSet(int initialCapacity) {
        super(initialCapacity, .75f, true);
    }
 public LinkedHashSet() {
        super(16, .75f, true);
    }
 public LinkedHashSet(Collection c) {
        super(Math.max(2*c.size(), 11), .75f, true);
        addAll(c);
    }

总结:LinkedHashSet是在HashSet的基础上用链表维护遍历出的顺序,但是并不能说其是有序的,因为在存储的时候,LinkedHashSet还是无序的,只是单独用一个链表来保证迭代出的顺序。

你可能感兴趣的:(JAVA那些事,Java集合框架)