目录
代码示例:
- /*Set集合的特点:
- * 1.可以去除重复的元素
- * 2.存取元素的顺序是不一致的
- * 3.set集合没有带索引的方法,所以不能使用普通for循环遍历、也不能通过索引获取、删除元素*/
- public class SetDemo_01 {
- public static void main(String[] args) {
- Set
set = new TreeSet<>(); - set.add("ccc");
- set.add("aaa");
- set.add("bbb");
- set.add("aaa");//存重复的元素,set集合会自动去重
-
- //增强for循环遍历
- for (String s1 : set) {
- System.out.println(s1);
- }
- System.out.println("------------------------------------");
-
- //迭代器循环遍历
- Iterator
it = set.iterator(); - while (it.hasNext()) {
- String s2 = it.next();
- System.out.println(s2);
- }
- }
- }
因此想要使用TreeSet集合,需要先制定排序规则。这里要说到自然排序Comparable的使用:
代码示例:
- public class TreeSetDemo_02 {
- public static void main(String[] args) {
- TreeSet
ts = new TreeSet<>(); -
- Student s1=new Student("ZhangSan",20);
- Student s2=new Student("LiSi",23);
- Student s3=new Student("WangEr",19);
- Student s4=new Student("ZhaoLiu",19);
-
- ts.add(s1);
- ts.add(s2);
- ts.add(s3);
- ts.add(s4);
-
- System.out.println(ts);
- }
- }
- /*自定义类要实现Comparable接口,并且这个接口的泛型,与TreeSet集合里的数据类型一致*/
- public class Student implements Comparable
{ - private String name;
- public int age;
-
- public Student() {
- }
-
- public Student(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
-
- @Override
- public int compareTo(Student o) {
- /* 自然排序的原理:
- 如果返回值是负数,表示当前存入的元素是较小值,存左边
- 如果返回值是0,表示当前存入的元素在TreeSet集合中已经存在,重复元素,不存
- 如果返回值是正数,表示当前存入的元素是较大值,存右边
- */
- //表示按照对象的年龄进行排序
- int result = this.age - o.age;//this.age代表当前正要存入的元素年龄,o.age代表已经存入进去的元素年龄,二者相减,再看返回值是正、0、还是为负
- result = result == 0 ? this.name.compareTo(o.getName()) : result;/*当年龄一样的时候,用姓名的首字母去排序;
- 注:this.name.compareTo中的compareTo方法是String类里的,
- 因为this.name是String类型的。*/
- return result;
- }
- }
比较器Comparator排序的使用步骤:
代码示例:
- public class TreeSetDemo_03 {
- public static void main(String[] args) {
- //使用匿名内部类进行排列
- TreeSet<Teacher> ts = new TreeSet<>(new Comparator<Teacher>() {
- @Override
- public int compare(Teacher o1, Teacher o2) {
- //o1代表的是要存入的元素
- //o2代表已经存入的元素
- int result = o1.getAge() - o2.getAge();//主要排序条件:年龄
- result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;//次要排序条件:姓名
- return result;
- }
- });
-
- Teacher s1 = new Teacher("ZhangSan", 20);
- Teacher s2 = new Teacher("LiSi", 23);
- Teacher s3 = new Teacher("WangEr", 19);
- Teacher s4 = new Teacher("ZhaoLiu", 19);
-
- ts.add(s1);
- ts.add(s2);
- ts.add(s3);
- ts.add(s4);
-
- System.out.println(ts);
- }
- }
- public class Teacher {
- private String name;
- private int age;
-
- public Teacher() {
- }
-
- public Teacher(String name, int age) {
- this.name = name;
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "Teacher{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
-
-
- }