美文网首页
开发基础知识,Java对象比较和排序

开发基础知识,Java对象比较和排序

作者: Sky_Blue | 来源:发表于2020-03-25 17:48 被阅读0次
    一、比较对象是否相等

    复写equals和hashCode方法

    public class Student  {
        private String name;
        private int age;
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Student)) return false;
            Student student = (Student) o;
            return getAge() == student.getAge() && SortUtils.equals(getName(), student.getName());
        }
    
        @Override
        public int hashCode() {
            return SortUtils.hashCode(getName(), getAge());
        }
    }
    
    
    public class SortUtils {
        public static boolean equals(Object a, Object b) {
            return (a == b) || (a != null && a.equals(b));
        }
    
        public static int hashCode(Object ... values) {
            return Arrays.hashCode(values);
        }
    }
    
    二、List集合排序方法一
    1. 实现 Comparable接口
    2. 调用 Collections.sort(list)进行排序
    public class Student implements Comparable<Student> {
        private String name;
        private int age;
        @Override
        public int compareTo(Student student) {
            if (student == null) {
                return 0;
            }
            // 按年龄从小到大排序
            int sort = this.age - student.age;
            if (sort == 0) {
                if (!TextUtils.isEmpty(this.name)&&!TextUtils.isEmpty(student.name)){
                    sort = this.name.compareTo(student.name);
                }
            }
            return sort;
        }
    }
    
    三、List集合排序方法二
    1. 自定义类,继承Comparator
    2. 调用 Collections.sort(list,comparator)进行排序
    public class StudentComparator implements Comparator<Student> {
        @Override
        public int compare(Student stu1, Student stu2) {
            if (stu1 == null || stu2 == null) {
                return 0;
            }
            // 按年龄从大到小排序
            int sort = stu2.getAge() - stu1.getAge();
            // 年龄一样,按姓名排序
            if (sort == 0) {
                if (!TextUtils.isEmpty(stu1.getName()) && !TextUtils.isEmpty(stu2.getName())) {
                    sort = stu1.getName().compareTo(stu2.getName());
                }
            }
            return sort;
        }
    }
    
    四、综合测试代码
        protected void test() {
            Student s1 = new Student("Lven", 18);
            Student s2 = new Student("Kven", 25);
            Student s5 = new Student("Lven", 19);
            Student s6 = new Student("Lven", 19);
            Student s3 = new Student("Rven", 25);
            Student s4 = new Student("Kven", 19);
    
            // 优先使用构造参数进行排序
            Set<Student> set = new TreeSet<>(new StudentComparator());
            set.add(s1);
            set.add(s1);
            set.add(s2);
            set.add(s5);
            set.add(s3);
            set.add(s6);
            set.add(s4);
            // 优先使用第二个参数进行排序
            // Collections.sort(list,new StudentComparator());
            for (Student student : set) {
                Log.e("MainActivity", student.toString());
            }
        }
    
    set排序结果.png

    相关文章

      网友评论

          本文标题:开发基础知识,Java对象比较和排序

          本文链接:https://www.haomeiwen.com/subject/lqexuhtx.html