标签:style blog http color java os io strong ar
注:以下源码基于jdk1.7.0_11
static final long serialVersionUID = -5024744406713321676L; private transient HashMap<E,Object> map;//内部封装了HashMap // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object();//值固定为new Object /*构造器照搬HashMap的*/ public HashSet() { map = new HashMap<>(); } public HashSet(Collection<? extends E> c) { map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16)); addAll(c); } public HashSet(int initialCapacity, float loadFactor) { map = new HashMap<>(initialCapacity, loadFactor); } public HashSet(int initialCapacity) { map = new HashMap<>(initialCapacity); } HashSet(int initialCapacity, float loadFactor, boolean dummy) { map = new LinkedHashMap<>(initialCapacity, loadFactor); }
public boolean add(E e) { return map.put(e, PRESENT)==null; }
public boolean remove(Object o) { return map.remove(o)==PRESENT; }
private transient NavigableMap<E,Object> m; // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object(); TreeSet(NavigableMap<E,Object> m) { this.m = m; } public TreeSet() {//内部是TreeSet this(new TreeMap<E,Object>()); } public TreeSet(Comparator<? super E> comparator) { this(new TreeMap<>(comparator)); } public TreeSet(Collection<? extends E> c) { this(); addAll(c); } public TreeSet(SortedSet<E> s) { this(s.comparator()); addAll(s); }这里需要注意的是,由于TreeSet不支持null键,故而TreeSet也是不支持null键的。这要跟HashSet区分开来。
public boolean add(E e) { return m.put(e, PRESENT)==null; } public boolean remove(Object o) { return m.remove(o)==PRESENT; }
标签:style blog http color java os io strong ar
原文地址:http://blog.csdn.net/chdjj/article/details/38783931