涉及到集合排序的问题,就要用到.sort()方法,对于集合的排序,有两个接口实现了集合中的排序问题:
package test002;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 用户类
* @author 黄小宝
*
*/
public class User implements Comparable{
//用户年龄
private int age;
//用户姓名
private String name;
public User() {
// TODO Auto-generated constructor stub
}
public User(int age, String name) {
super();
this.age = age;
this.name = name;
}
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;
}
//重写equals方法
@Override
public String toString() {
return "User [age=" + age + ", name=" + name + "]";
}
//重写接口中的方法,使得按照年龄的大小进行排序
@Override
public int compareTo(User u) {
return this.age-u.getAge();
}
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(new User(25, "张三"));
list.add(new User(18, "李四"));
list.add(new User(24, "王五"));
list.add(new User(32, "关羽"));
list.add(new User(47, "张飞"));
list.add(new User(27, "刘备"));
Collections.sort(list);
for(User u:list) {
System.out.println(u);
}
}
}
输出结果:
User [age=18, name=李四]
User [age=24, name=王五]
User [age=25, name=张三]
User [age=27, name=刘备]
User [age=32, name=关羽]
User [age=47, name=张飞]
在一个集合中,如果集合中的元素类型没有实现comparable接口,那么就可以通过这种方式进行自定义排序规则(仅限于本类中),然后通过:collections.sort(list)进行排序,如果list中元素所在的类已经默认实现了接口,那么就会按照默认的方式进行排序。
package test002;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 用户类
* @author 黄小宝
*
*/
public class User implements Comparable{
//用户年龄
private int age;
//用户姓名
private String name;
public User() {
// TODO Auto-generated constructor stub
}
public User(int age, String name) {
super();
this.age = age;
this.name = name;
}
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;
}
//重写equals方法
@Override
public String toString() {
return "User [age=" + age + ", name=" + name + "]";
}
}
对于一个User类型的集合
List list = new ArrayList<>();
list.add(new User(25, "张三"));
list.add(new User(18, "李四"));
list.add(new User(24, "王五"));
list.add(new User(32, "关羽"));
list.add(new User(47, "张飞"));
list.add(new User(27, "刘备"));
我们对他进行排序,因为这个User类没有实现排序的接口,所以我们可以定义一个类来实现comparator接口,代码如下:
package test002;
import java.util.Comparator;
public class MyComparator implements Comparator{
@Override
public int compare(User u1, User u2) {
return u1.getAge()-u2.getAge();
}
}
那么我们就可以实现对集合list的排序了:
package test002;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(new User(25, "张三"));
list.add(new User(18, "李四"));
list.add(new User(24, "王五"));
list.add(new User(32, "关羽"));
list.add(new User(47, "张飞"));
list.add(new User(27, "刘备"));
list.sort(new MyComparator());//注:此处也可以用匿名内部类,拉姆达表达式实现
for(User u:list) {
System.out.println(u);
}
}
}
输出结果:
User [age=18, name=李四]
User [age=24, name=王五]
User [age=25, name=张三]
User [age=27, name=刘备]
User [age=32, name=关羽]
User [age=47, name=张飞]
好了,以上纯属个人观点,如有错误,还请指出!!!