HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<>(initialCapacity, loadFactor);
} 这个构造函数是包访问权限,且底层使用LinkedHashMap来实现,所以LinkedHashSet是通过调用这个构造函数来获取实例而保持元素的插入顺序的,源代码如下:
public class LinkedHashSet继续编写上一个测试程序,将extends HashSet implements Set , Cloneable, java.io.Serializable { private static final long serialVersionUID = -2851667679971038690L; 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); } }
Set改为hashSet = new HashSet ();
Set后,输出结果发现,和插入的顺序一致。hashSet = new LinkedHashSet ();
3、TreeSet
TreeSet类不仅实现了Set接口,还实现了java.util.SortedSet接口,从而保证在遍历集合时按照递增的顺序获得对象。遍历对象时可能是按照自然顺序递增排列,因此存入用TreeSet类实现的Set集合的对象必须实现Comparable接口;也可能是按照指定比较器递增排序,即可以通过比较器对用TreeSet类实现的Set集合中的对象进行排序。
下面先看 TreeSet 类的部分源代码:
public class TreeSetextends AbstractSet implements NavigableSet , Cloneable, java.io.Serializable{ // 使用 NavigableMap 的 key 来保存 Set 集合的元素 private transient NavigableMap m; // 使用一个 PRESENT 作为 Map 集合的所有 value private static final Object PRESENT = new Object(); //----------构造函数------------------------------- // 包访问权限的构造器,以指定的 NavigableMap 对象创建 Set 集合 TreeSet(NavigableMap m) { this.m = m; } // 以自然排序方式创建 public TreeSet() { this(new TreeMap ()); } // 以指定的排序方式创建 public TreeSet(Comparator comparator) { this(new TreeMap<>(comparator)); } public TreeSet(Collection c) { this(); // 调用无参的默认构造器 addAll(c); // 添加Collection中已有的元素 } // 以SortedSet为基础创建TreeSet public TreeSet(SortedSet s) { this(s.comparator()); addAll(s); } }
提供了获取部分元素的方法如下:
public NavigableSetsubSet(E fromElement, boolean fromInclusive,E toElement, boolean toInclusive) { return new TreeSet<>(m.subMap(fromElement, fromInclusive,toElement, toInclusive)); } public NavigableSet headSet(E toElement, boolean inclusive) { return new TreeSet<>(m.headMap(toElement, inclusive)); } public NavigableSet tailSet(E fromElement, boolean inclusive) { return new TreeSet<>(m.tailMap(fromElement, inclusive)); } public SortedSet subSet(E fromElement, E toElement) { return subSet(fromElement, true, toElement, false); } public SortedSet headSet(E toElement) { return headSet(toElement, false); } public SortedSet tailSet(E fromElement) { return tailSet(fromElement, true); }
从如上的几个构造器可以看出,其实底层还是通过TreeMap来实现的。
public class Person implements Comparable{
private String name;
private long id_card;
public Person(String name,long id_card){
this.name = name;
this.id_card = id_card;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public long getId_card(){
return id_card;
}
public void setId_card(long id_card){
this.id_card = id_card;
}
public int compareTo(Object o){//默认按编号升序排序
Person person = (Person)o;
int result = id_card>person.id_card 1:(id_card==person.id_card 0:-1);
return result;
}
}
public class TestSet{
public static void main(String args[]){
TreeSet
treeSet = new TreeSet
(); Person p1 = new Person("马先生",22015); Person p2 = new Person("李先生",22016); Person p3 = new Person