Java集合框架--List 排序
一、Java List 类
二、Java List 排序
三、Java List 遍历
四、Java List 去重
五、Java List 转Array
二、Java List排序(两种方法)
1. 利用Collections类的 java.util.Collections.sort(java.util.List, java.util.Comparator) 方法,自定义比较器对象对指定对象进行排序
对Person类对象按照起身高(降序)排序,当升高相同按id升序
// Person 类
class Person{
private int id;
private String name;
private float tall;
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public float getTall() {return tall;}
public void setTall(float tall) {this.tall = tall;}
}
//比较器
class PersonComparator implements Comparator<Person>{
public int compare(Person p1, Person p2 ) {
if ( p1.getTall() > p2.getTall() ) return -1;
else if ( p1.getTall() == p2.getTall() ) {
if (p1.getId() > p2.getId() ) return 1;
else if ( p1.getId() == p2.getId() ) return 0;
else return -1;
}
else return 1;
}
}
public class listSort {
public static void main(String[] args) {
// TODO Auto-generated method
Person p1 = new Person();
p1.setId(005); p1.setName("Jerry"); p1.setTall(165);
Person p2 = new Person();
p2.setId(003); p2.setName("Morty"); p2.setTall(171.1f);
Person p3 = new Person();
p3.setId(001); p3.setName("Rick"); p3.setTall(176);
Person p4 = new Person();
p4.setId(004); p4.setName("Tom"); p4.setTall(171.1f);
List<Person> list = new ArrayList<>();
list.add(p1); list.add(p4); list.add(p3); list.add(p2);
System.out.println("*****before sort*****");
for(Person ps:list)
System.out.println("id: "+ps.getId()+ " name: "+ps.getName() + " score: " + ps.getTall() );
Collections.sort(list, new PersonComparator());
System.out.println("*****after sort*****");
for(Person ps:list)
System.out.println("id: "+ps.getId()+ " name: "+ps.getName() + " score: " + ps.getTall() );
}
}
运行结果
*****before sort*****
id: 5 name: Jerry score: 165.0
id: 4 name: Tom score: 171.1
id: 1 name: Rick score: 176.0
id: 3 name: Morty score: 171.1
*****after sort*****
id: 1 name: Rick score: 176.0
id: 3 name: Morty score: 171.1
id: 4 name: Tom score: 171.1
id: 5 name: Jerry score: 165.0
2. 利用Collections类的 java.util.Collections.sort(java.util.List, java.util.Comparator) 方法,自定义比较器对象对指定对象进行排序
对一个Person类,让其按照一个Order属性进行排序,我们可以让Person类实现Comparable接口,重写其CompareTo方法即可,如:让Person按照身高属性升序排序。
class Person implements Comparable<Person>{
private String name;
private Integer tall; //compareTo只能是对象之间的比较,不能适用于int等基本类型,要使用int的封装类型Integer
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public Integer getTall() {return tall;}
public void setTall(Integer tall) {this.tall = tall;}
public int compareTo(Person ps) { //compareTo只能是对象之间的比较,不能适用于int boolean等基本类型
return this.getTall().compareTo( ps.getTall() ); //这里定义排序规则
}
}
public class listSort2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Person> list = new ArrayList<>();
Person p1 = new Person(); Person p2 = new Person(); Person p3 = new Person();
p1.setName("Rick"); p1.setTall(174);
p2.setName("Morty"); p2.setTall(178);
p3.setName("Jerry"); p3.setTall(176);
list.add(p1); list.add(p2); list.add(p3);
//排序
Collections.sort(list);
for (Person p : list) {
System.out.println(p.getName());
}
}
}
运行结果
Rick
Jerry
Morty