赞
踩
集合不像数组那样“单纯”,操作起来自然没有数组那样方便一些,但是正是这样,也为集合提供了多种灵活的方式,对集合中的元素进行排序。接下来我分别从两个大方向来演示集合中元素排序。第一种是需要实现Comparator接口。然后重写里面的compare方法。
请看代码:
下面的代码是按照年龄来进行排序。
1、定义一个Person类
public class Person{ private int age; private String name; private String addr; public Person(String name, int age, String addr) { this.age = age; this.name = name; this.addr = addr; } public Person() { } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Override public String toString() { return "姓名:"+getName()+" 年龄:"+getAge()+" 住址:"+getAddr(); } }
2、定义一个PersonComparator类用来实现Comparator接口
import java.util.Comparator;
public class PersonComparator implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
}
3、主程序代码:
package com.qst.dms.entity; import java.util.ArrayList; import java.util.Comparator; public class CollectionDemo { public static void main(String[] args) { ArrayList<Person> list = new ArrayList<>(); ArrayList<Person> list1 = new ArrayList<>(); ArrayList<Person> list2 = new ArrayList<>(); list.add(new Person("小米",8,"北京")); list.add(new Person("魅族",17,"珠海")); list.add(new Person("华为",32,"深圳")); list.add(new Person("荣耀",5,"深圳")); list.add(new Person("一加",4,"深圳")); list1.add(new Person("张三",12,"北京")); list1.add(new Person("李四",3,"上海")); list1.add(new Person("王五",23,"深圳")); list1.add(new Person("马六",2,"东京")); list2.add(new Person("小明",12,"北京")); list2.add(new Person("小李",3,"上海")); list2.add(new Person("小红",23,"深圳")); list2.add(new Person("小白",2,"东京")); //使用函数式接口 System.out.println("list排序前:"); list.forEach(e -> System.out.println(e)); System.out.println("---------函数式接口排序--------"); list.sort(new PersonComparator()); list.forEach(e -> System.out.println(e)); System.out.println(); //使用Lambda表达式 System.out.println("list1排序前:"); list1.forEach(e -> System.out.println(e)); System.out.println("---------Lambda表达式排序------"); list1.sort((a,b) ->a.getAge() - b.getAge()); list1.forEach(e -> System.out.println(e)); System.out.println(); //使用匿名内部类,这种方法不用定义PersonComparator类 System.out.println("list2排序前:"); list2.forEach(e -> System.out.println(e)); System.out.println("---------使用匿名内部类进行排序------"); list2.sort(new Comparator<Person>(){ @Override public int compare(Person o1, Person o2) { return o1.getAge() - o2.getAge(); } }); System.out.println("排序后:"); list2.forEach(e -> System.out.println(e)); } }
在输出的集合中的每个元素的时候,采用任然是Lambda表达式,关于Iterator遍历,请看我的另一篇博文
相比之下,定义了PersonComparator类后,使用函数式接口比使用Lambda表达式要显得更简单一些。
对于匿名内部类,就是在sort方法中直接new一个Comparator的匿名对象,并且重写compare方法。记住,这是一个泛型的接口,所以一定要在后面用尖括号指明是为哪一个类进行排序的。
运行截图:
第二种方法:实现comparable接口,这是Collections工具类为集合提供排序用的。
Persons类:
package com.qst.dms.entity; public class Persons implements Comparable<Persons>{ private int age; private String name; public Persons( String name,int age) { this.age = age; this.name = name; } public Persons() { } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int compareTo(Persons o) { if(this.age < o.age){ return -1; }else if(this.age == o.age){ return 0; }else{ return 1; } } @Override public String toString() { return "Persons{" + "age=" + age + ", name='" + name + '\'' + '}'; } }
主程序代码:
package com.qst.dms.entity; import java.util.ArrayList; import java.util.Collections; public class PersonsDemo { public static void main(String[] args) { ArrayList<Persons> list = new ArrayList<>(); list.add(new Persons("小米",8)); list.add(new Persons("魅族",17)); list.add(new Persons("华为",32)); list.add(new Persons("荣耀",5)); list.add(new Persons("一加",4)); System.out.println("排序前:"); list.forEach(e -> System.out.println(e)); Collections.sort(list); System.out.println("排序后:"); list.forEach(e -> System.out.println(e)); } }
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。