简单结合示例展示:
测试类:
1 package set; 2 import java.util.Comparator; 3 import java.util.HashSet; 4 import java.util.Iterator; 5 import java.util.LinkedHashSet; 6 import java.util.Set; 7 import java.util.TreeSet; 8 /** 9 * 使用TreeSet存储 多个学生的信息 10 * TreeSet 自然顺序,唯一 11 * 12 * Student类实现 comparable接口,是内部比较器-------缺点:如果要修改比较规则,那么compareTo 都要按照需求来改(麻烦) 13 * 可以使用外部比较器实现 ,定义多个比较器,想用哪个就用哪个 14 * 使用外部比较器 还可以使用匿名内部类 15 * 按照 分数排序,如果分数一致,再按照学号排序 16 * 17 * Comparable是内部比较器 18 * Comparator是外部比较器 19 * 20 * hashCode 和 equlas 有什么神奇的作用 21 * hashCode:计算哈希码,是整数,根据哈希码计算数据在哈希表存储的位置 22 * equlas: 添加数据出现冲突(位置相同有元素),需要equlas比较,判断元素内容是否相同 23 * 查询的时候也需要 equlas比较,判断元素内容是否相同 24 * 25 * 各种数据类型的哈希码是如何获取的 26 * int 取自身 27 * double 3.14 3.145 看double 源码 28 * String abc a:97 b:98 c:99 29 * cba 30 * cab 31 * hash= a+b+c; a*1 + b*2 + c*3; 32 * hash= c+b+a; c*1 + b*2 + a*3; 33 * hash= c+a+b; c*1 + a*2 + b*3 34 * Student 拿到每一个属性的哈希码,进行相加或者相乘的运算 35 * @author superdrew 36 */ 37 public class TestSet3 { 38 public static void main(String[] args) { 39 Comparator<Student> stuAgeDesc = new StuAgeComparator(); 40 Comparator<Student> stuScoreDesc = new StuScoreComparator(); 41 42 //比较器只使用一次-----没必要新建一个类 -----所以可使用匿名内部类 43 /*Comparator<Student> stuNameDesc = new Comparator<Student>() { 44 public int compare(Student stu1, Student stu2) { 45 return stu1.getName().compareTo(stu2.getName()); 46 } 47 };*/ 48 49 TreeSet<Student> stuSet =new TreeSet<>(stuScoreDesc); 50 stuSet.add(new Student(3, "刘亦菲", 23, 88)); 51 stuSet.add(new Student(4, "林志玲", 20, 88)); 52 stuSet.add(new Student(1, "范冰冰", 22, 88)); 53 stuSet.add(new Student(2, "张靓颖", 21, 90)); 54 System.out.println("学生人数:" + stuSet.size()); //4 ? 6 55 //System.out.println(stuSet); //4 56 System.out.println("排序规则是:先按照score排序,再按照id排序"); 57 for (Student student : stuSet) { 58 System.out.println(student); 59 } 60 } 61 }
实体类:
1 package set; 2 3 public class Student { 4 private int id; 5 private String name; 6 private int age; 7 private double score; 8 9 /** 10 * 内部比较器 11 * 按照学号排序 12 * 返回值 13 * >0 正数,前面的数值比后面大 14 * <0 负数,前面的数值比后面小 15 * =0 前面的数值与后面的值相等 16 */ 17 /*public int compareTo(Student stu) { 18 //return this.id - stu.id; 19 return this.name.compareTo(stu.name); 20 }*/ 21 22 public Student(int id, String name, int age, double score) { 23 super(); 24 this.id = id; 25 this.name = name; 26 this.age = age; 27 this.score = score; 28 } 29 30 public Student() { 31 super(); 32 } 33 34 public int getId() { 35 return id; 36 } 37 38 public void setId(int id) { 39 this.id = id; 40 } 41 42 public String getName() { 43 return name; 44 } 45 46 public void setName(String name) { 47 this.name = name; 48 } 49 50 public int getAge() { 51 return age; 52 } 53 54 public void setAge(int age) { 55 this.age = age; 56 } 57 58 public double getScore() { 59 return score; 60 } 61 62 public void setScore(double score) { 63 this.score = score; 64 } 65 66 @Override 67 public String toString() { 68 return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + score + "]"; 69 } 70 71 @Override 72 public int hashCode() { 73 final int prime = 31; 74 int result = 1; 75 result = prime * result + age; 76 result = prime * result + id; 77 result = prime * result + ((name == null) ? 0 : name.hashCode()); 78 long temp; 79 temp = Double.doubleToLongBits(score); 80 result = prime * result + (int) (temp ^ (temp >>> 32)); 81 return result; 82 } 83 84 @Override 85 public boolean equals(Object obj) { 86 if (this == obj) 87 return true; 88 if (obj == null) 89 return false; 90 if (getClass() != obj.getClass()) 91 return false; 92 Student other = (Student) obj; 93 if (age != other.age) 94 return false; 95 if (id != other.id) 96 return false; 97 if (name == null) { 98 if (other.name != null) 99 return false; 100 } else if (!name.equals(other.name)) 101 return false; 102 if (Double.doubleToLongBits(score) != Double.doubleToLongBits(other.score)) 103 return false; 104 return true; 105 } 106 107 }
外部比较器1:根据学生的成绩升序排序:
1 package set; 2 3 import java.util.Comparator; 4 5 public class StuScoreComparator implements Comparator<Student>{ 6 /** 7 * 按照分数排序 8 */ 9 public int compare(Student stu1, Student stu2) { 10 int result = 0; 11 if(stu1.getScore() > stu2.getScore()){ 12 result = 1; 13 }else if(stu1.getScore() < stu2.getScore()){ 14 result = -1; 15 }else{ 16 //如果分数一致,再按照ID排序 17 result = stu1.getId() - stu2.getId(); 18 } 19 return result; 20 //return stu1.getAge() - stu2.getAge(); 21 } 22 23 }
外部比较器2:根据学生的编号降序排序:
1 package set; 2 3 import java.util.Comparator; 4 5 public class StuAgeComparator implements Comparator<Student>{ 6 7 /** 8 * 按照年龄排序 9 */ 10 public int compare(Student stu1, Student stu2) { 11 //先按年龄排序,如果年龄相同,再按照姓名排序 12 if(stu1.getAge() - stu2.getAge() !=0){ 13 return stu1.getAge() - stu2.getAge(); 14 }else{ 15 return stu1.getName().compareTo(stu2.getName()); 16 } 17 } 18 19 }