LinkedHashSet 源码可以精简如下:
public class LinkedHashSet<E> extends HashSet<E> { public LinkedHashSet() { super(16, .75f, true); } } 其中,super 关键字调用的是父类中 "only used by LinkedHashSet" 的构造函数,如下:
public class HashSet<E> { /** * Constructs a new, empty linked hash set. (This package private * constructor is only used by LinkedHashSet.) The backing * HashMap instance is a LinkedHashMap with the specified initial * capacity and the specified load factor. */ HashSet(int initialCapacity, float loadFactor, boolean dummy) { map = new LinkedHashMap<>(initialCapacity, loadFactor); } } 在这样的设计下,HashSet 需要额外携带一部分不属于它的代码,实在不够优雅。
