list自定义排序

2021/5/9 18:29:06

本文主要是介绍list自定义排序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

compareTo()方法:a.compareTo(b),如果a<b return -1,如果 a>b return 1   在下列的排序中,如果a<b,则返回-1,正常递增排序,如果a>b,返回1,则a和b的位置替换下。

public class EqualsAndHashCode {
    private List<Student>list;
    @Test
    public void testCompare(){
        System.out.println("list = " + list);
        list.sort((Student o1,Student o2)->{
            /**规则:返回值<0  o1,o2....正常递增顺序
             * 当o1==null时,o2!=0时,如果我希望null在前面,则值<0
             * 当o1!=null,o2==null,时,null在前面,则让值>0,反之亦然*/
             /**正常递增顺序写法1*/
             // o1==null?(o2==null?0:-1):(o2==null?1:o1.getAge()-o2.getAge());
             // o1==null?(o2==null?0:Integer.MIN_VALUE):(o2==null?Integer.MAX_VALUE:o1.getAge().compareTo(o2.getAge()))
              if(o1==o2) return 0;
              if(o1==null){
                  return -1;
              }else if(o2==null){
                  return 1;
              }else {
                  return o1.getAge()-o2.getAge();
              }
        });
        System.out.println("排序后===========================排序后");
        System.out.println(list);

    }

    @Before
    public void init(){
        list=new LinkedList<>();
        list.add(null);
        list.add(new Student(0,"第零",new User("baiuser")));
        list.add(new Student(1,"第一",new User("baiuser")));
        list.add(null);
        list.add(new Student(2,"第二",new User("zhaowen")));
        list.add(new Student(4,"第三",new User("wangmancang")));
        list.add(new Student(3,"第四",new User("wangmancang")));
        list.add(null);
    }
}
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
class Student{
    private Integer age;
    private String name;
    private User user;
}
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
class User{
    private String userName;
}

数据输出:

list = [null, Student(age=0, name=第零, user=User(userName=baiuser)), Student(age=1, name=第一, user=User(userName=baiuser)), null, Student(age=2, name=第二, user=User(userName=zhaowen)), Student(age=4, name=第三, user=User(userName=wangmancang)), Student(age=3, name=第四, user=User(userName=wangmancang)), null]
排序后===========================排序后
[null, null, null, Student(age=0, name=第零, user=User(userName=baiuser)), Student(age=1, name=第一, user=User(userName=baiuser)), Student(age=2, name=第二, user=User(userName=zhaowen)), Student(age=3, name=第四, user=User(userName=wangmancang)), Student(age=4, name=第三, user=User(userName=wangmancang))]

方法二:

使用 Collections.sort方法排序,缺陷,当this对象为null时,会报空指针异常

public class EqualsAndHashCode {
    private List<Student>list;
    @Test
    public void testCompare(){
        Collections.sort(list);
        System.out.println(list);
    }

    @Before
    public void init(){
        list=new LinkedList<>();
        list.add(null);
        list.add(new Student(0,"第零",new User("baiuser")));
        list.add(new Student(1,"第一",new User("baiuser")));
        list.add(new Student(2,"第二",new User("zhaowen")));
        list.add(new Student(4,"第三",new User("wangmancang")));
        list.add(new Student(3,"第四",new User("wangmancang")));
       // list.add(null);
    }
}
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
class Student implements Comparable<Student>{
    private Integer age;
    private String name;
    private User user;

    @Override
    public int compareTo(Student o) {
        if(this==o){
            return 0;
        }else if(null==this){
            return -1;
        }else if(null==o){
            return 1;
        }else {
           // System.out.println();
            return age-o.age;
        }

    }
}
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
class User{
    private String userName;
}

解决方法:在方法中直接指定比较器,compare方法可以去掉compareTo()方法的弊端

 @Test
    public void testCompare(){
        Collections.sort(list,(Student o1,Student o2)->{
            return o1==null?(o2==null?0:-1):(o2==null?1:o1.getAge()-o2.getAge());
        });
        System.out.println(list);
    }

 



这篇关于list自定义排序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程