标签:final navig res shm nbsp 要求 add color 常量
TreeSet是一种排序的set,实现了SortedSet接口,它的元素要求实现Comparable接口
private transient NavigableMap<E,Object> m; private static final Object PRESENT = new Object(); TreeSet(NavigableMap<E,Object> m) { this.m = m; } public TreeSet() { this(new TreeMap<E,Object>());//默认为TreeMap } public boolean add(E e) { return m.put(e, PRESENT)==null; }
实际上是基于TreeMap实现的,key存入元素,value存入常量Object对象
HashSet和TreeSet几乎一样,区别在于底层是HashMap实现
private transient HashMap<E,Object> map; private static final Object PRESENT = new Object(); public HashSet() { map = new HashMap<>(); } public boolean add(E e) { return map.put(e, PRESENT)==null; }
标签:final navig res shm nbsp 要求 add color 常量
原文地址:https://www.cnblogs.com/jiaqirumeng/p/9064255.html