Set集合的类型
- Set
特点:唯一、无序 - HashSet
1.采用HashTable哈希表存储结构。
2.优点:添加快、删除快、查询快。
3.缺点:无序 - LinkedHashSet
1.采用哈希表存储结构,同时使用链表维护次序。
2.有序(添加顺序)。
- TreeSet
1.采用红黑树的存储结构。
2.优点:有序(自然顺序)、查询速度比List快(按照内容查询)
3.缺点:查询速度没有HashSet快
使用Set集合存储课程名称
public class TestSet1 {public static void main(String[] args) {//创建一个集合set对象//Set<String> set = new HashSet<String>();//Set<String> set = new LinkedHashSet<String>();Set<String> set = new TreeSet<String>();//添加多个课程set.add(new String("Java"));set.add("Oracle");set.add("HTML");set.add(new String("Java"));//输出课程System.out.println(set.size());System.out.println(set);//不可以使用for循环遍历setfor(int i=0;i<set.size();i++){//set.get(i);}//支持增强的for循环//支持IteratorIterator<String> it = set.iterator();while(it.hasNext()){//System.out.println(it.next());} }
}
总结
- HashSet 哈希表 唯一 无序
- LinkedHashSet 哈希表+链表 唯一 有序(添加顺序)
- TreeSet 红黑树 一种二叉平衡树 唯一 有序(自然顺序)
- List针对Collection增加了一些关于索引位置操作的方法 get(i)
add(i,elem),remove(i),set(i,elem) - Set是无序的,不可能提供关于索引位置操作的方法,set针对Collection没有增加任何方法
- List的遍历有三种方式:for循环、for-each循环、Iterator迭代器
- Set的遍历有两种方式: for-each循环、Iterator迭代器
使用Set存储多个自定义学生信息
public class TestSet2 {public static void main(String[] args) {//创建一个集合set对象//Set<Student> set = new TreeSet<Student>();//Set<Student> set = new HashSet<Student>();Set<Student> set = new LinkedHashSet<Student>();//添加多个学生Student stu2 = new Student(2, "lisi", 23, 98);Student stu3 = new Student(3, "wangwu", 22, 87);Student stu1 = new Student(1, "zhangsan", 23, 90);Student stu4 = new Student(1, "zhangsan", 23, 90);set.add(stu1);set.add(stu2);set.add(stu3);set.add(stu4); //输出学生System.out.println(set.size());System.out.println(set);}
}
- 问题1:HashSet、LinkedHashSet :为什么String有重复,会保持唯一;为什么Student有重复,不会保持唯一。
- 问题2:TreeSet 为什么String可以添加,而Student就不让添加到TreeSet中呢? 而是抛出异常:java.lang.ClassCastException: com.bjsxt.entity.Student cannot be cast to java.lang.Comparable
- 思考:String是系统类,Student是自定义类,应该是String已经做了某些事情,但是Student没有做
- 解答1:HashSet、LinkedHashSet 需要Student实现hashCode()和equals()
- 解答2:TreeSet 需要Student实现Comparable接口并指定比较的规则
Set集合的原理
外部比较器Comparator的作用及使用
- 问题:内部比较器只有一个,如果希望指定多种比较的规则,怎么办?
- 解决:可以定义多个外部比较器,定义额外的类实现Comparator接口。
示例:定义外部比较器,按照分数升序排列
public class StudentScoreComparator implements Comparator<Student> {@Overridepublic int compare(Student stu1, Student stu2) {if(stu1.getScore()> stu2.getScore()){return 1;}else if(stu1.getScore()<stu2.getScore()){return -1;}else{return 0;} }
}
定义外部比较器:按照姓名逆序排序,如姓名相同,按学号逆序排列
public class StudentNameDescComparator implements Comparator<Student> {@Overridepublic int compare(Student stu1, Student stu2) {int n = -stu1.getName().compareTo(stu2.getName());if(n !=0){return n;}else{return -(stu1.getSno()-stu2.getSno());}}
}
使用外部比较器实现TreeSet对学生的排序
public class TestSet3 {public static void main(String[] args) {//创建一个set集合对象// Comparator comp = new StudentScoreComparator();//Comparator comp = new StudentNameComparator();Comparator comp = new Comparator<Student>() {@Overridepublic int compare(Student stu1, Student stu2) {return -(stu1.getSno()-stu2.getSno());}};
//没有指定比较器,使用内部比较器//Set<Student> set = new TreeSet<Student>();
//指定了外部比较器,就使用外部比较器Set<Student> set = new TreeSet<Student>(comp); //使用set存储多个学生数据Student stu1 = new Student(1,"xiaohua",18,90);Student stu4 = new Student(4,"xiaoli",18,60);Student stu5 = new Student(1,"xiaohua",18,90);Student stu2 = new Student(2,"xiaoming",17,80);Student stu3 = new Student(3,"xiaozhang",14,90);set.add(stu3);set.add(stu1);set.add(stu2);set.add(stu4);set.add(stu5);//输出学生数据System.out.println(set.size());//??for(Student stu :set){System.out.println(stu);}
}
}
内部比较器只能定义一个,一般将使用频率最高的比较规则定义为内部比较器的规则;外部比较器可以定义多个;
注意1:对于外部比较器,如果使用次数较少,可以通过匿名内部类来实现。
注意2:需要比较的场合才需要实现内部比较器或者外部比较器,比如排序、比如TreeSet中数据的存储和查询,在HashSet、LinkedHashSet、ArrayList中存储元素,不需要实现内部比较器或者外部比较器。
理解HashSet的源码
- HashSet的底层使用的是HashMap,所以底层也是哈希表。
- HashSet的元素到HashMap中做key,value统一是new Object()。
public class HashSet<E> implements Set<E>{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;} public int size(){return map.size();}public Iterator<E> iterator(){return map.keySet.iterator();}
}
理解TreeSet的源码
- TreeSet底层使用的是TreeMap,所以底层也是红黑树。
- TreeSet的元素e是作为TreeMap的key存在的,value统一为new Object()。
public class TreeSet<E> implements NavigableSet<E>{//底层的TreeMap的引用private transient NavigableMap<E,Object> m;private static final Object PRESENT=new Object();public TreeSet(){//创建TreeSet对象就是创建TreeMap对象this(new TreeMap<E,Object>());}TreeSet(NavigableMap<E,Object> m){this.m=m;}public boolean add(E e){return m.put(e,PRESENT)==null;}public int size(){return map.size();}
}