这期内容当中小编将会给大家带来有关Java中怎么对List集合排序,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站制作、成都做网站、郸城网络推广、成都小程序开发、郸城网络营销、郸城企业策划、郸城品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们大的嘉奖;成都创新互联公司为所有大学生创业者提供郸城建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com1.使用 Collections 工具类中的 sort() 方法
参数不同:
void sort(List list) 在自定义类User里面实现Comparable<User>接口,并重写抽象方法compareTo(Student o);
void sort(List list, Comparator c) 第二个参数为了省事,可以直接使用匿名内部类
public class User implements Comparable<User>{ private int score; private int age; public User(int score, int age){ super(); this.score = score; this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int compareTo(User o) { int i = this.getAge() - o.getAge();//先按照年龄排序 if(i == 0){ return this.score - o.getScore();//如果年龄相等了再用分数进行排序 } return i; } } public static void main(String[] args) { List<User> users = new ArrayList<User>(); users.add(new User(78, 26)); users.add(new User(67, 23)); users.add(new User(34, 56)); users.add(new User(55, 23)); Collections.sort(users); for(User user : users){ System.out.println(user.getScore() + "," + user.getAge()); } }
public class Students { private int age; private int score; public Students(int age, int score){ super(); this.age = age; this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } } public static void main(String[] args) { List<Students> students = new ArrayList<Students>(); students.add(new Students(23, 100)); students.add(new Students(27, 98)); students.add(new Students(29, 99)); students.add(new Students(29, 98)); students.add(new Students(22, 89)); Collections.sort(students, new Comparator<Students>() { @Override public int compare(Students o1, Students o2) { int i = o1.getScore() - o2.getScore(); if(i == 0){ return o1.getAge() - o2.getAge(); } return i; } }); for(Students stu : students){ System.out.println("score:" + stu.getScore() + ":age" + stu.getAge()); } }
2.直接使用list.sort()方法,传入实现Comparator接口的实现类的实例,为了省事直接传入匿名内部类
public class Students { private int age; private int score; public Students(int age, int score){ this.age = age; this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; }}public static void main(String[] args) { List<Students> students = new ArrayList<Students>(); students.add(new Students(23, 100)); students.add(new Students(27, 98)); students.add(new Students(29, 99)); students.add(new Students(29, 98)); students.add(new Students(22, 89)); students.sort(new Comparator<Students>() { @Override public int compare(Students o1, Students o2) { int i = o1.getScore() - o2.getScore(); if (i == 0) { return o1.getAge() - o2.getAge(); } return i; } }); for (Students stu : students) { System.out.println("score:" + stu.getScore() + ":age" + stu.getAge()); }}
上述就是小编为大家分享的Java中怎么对List集合排序了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
名称栏目:Java中怎么对List集合排序-创新互联
标题URL:https://www.cdcxhl.com/article40/degceo.html
成都网站建设公司_创新互联,为您提供微信公众号、网站收录、微信小程序、网站内链、定制开发、手机网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联