864
搜狐
集合排序
概述:本示例實現對象按年齡升序 人氣升序排序功能 姓名升序 降序排序功能
package ch02;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @author YaoShiyou 實現對象排序
*
*/
public class Person {
// 姓名
private String name;
// 年齡
private int age;
// 人氣
private int hobby;
public Person(String name) {
this.name = name;
}
public Person(String name, int age) {
this(name);
this.age = age;
}
public Person(String name, int age, int hobby) {
this(name, age);
this.hobby = hobby;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHobby() {
return hobby;
}
public void setHobby(int hobby) {
this.hobby = hobby;
}
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
list.add(new Person("g1", 18, 122));
list.add(new Person("g2", 17, 244));
list.add(new Person("g3", 45, 243));
list.add(new Person("g4", 9, 67));
list.add(new Person("g5", 98, 2));
System.out.println("排序前---");
for (Person person : list) {
System.out.println(person.getName() + "/t" + person.getAge() + "/t"
+ person.getHobby());
}
// Collections調用sort 方法 參數為
Collections.sort(list, new AgeComparatorAsc());
System.out.println("年齡排序後----");
for (Person person : list) {
System.out.println(person.getName() + "/t" + person.getAge() + "/t"
+ person.getHobby());
}
Collections.sort(list, new hobbyComparatorAsc());
System.out.println("人氣排序後----");
for (Person person : list) {
System.out.println(person.getName() + "/t" + person.getAge() + "/t"
+ person.getHobby());
}
Collections.sort(list, new NameComparatorAsc());
System.out.println("姓名升序排序後----");
for (Person person : list) {
System.out.println(person.getName() + "/t" + person.getAge() + "/t"
+ person.getHobby());
}
Collections.sort(list, new NameComparatorDesc());
System.out.println("姓名降序排序後----");
for (Person person : list) {
System.out.println(person.getName() + "/t" + person.getAge() + "/t"
+ person.getHobby());
}
}
}
// 實現比較器接口 采用Age升序排列
class AgeComparatorAsc implements Comparator<Person> {
public int compare(Person p1, Person p2) {
if (p1.getAge() > p2.getAge()) {
return 1;
} else if (p1.getAge() == p2.getAge()) {
return 0;
} else {
return -1;
}
}
}
class hobbyComparatorAsc implements Comparator<Person> {
public int compare(Person o1, Person o2) {
if (o1.getHobby() > o2.getHobby()) {
return 1;
} else if (o1.getHobby() == o2.getHobby()) {
return 0;
} else {
return -1;
}
}
}
class NameComparatorAsc implements Comparator<Person> {
public int compare(Person o1, Person o2) {
return o1.getName().compareTo(o2.getName());
}
}
class NameComparatorDesc implements Comparator<Person> {
public int compare(Person o1, Person o2) {
return o2.getName().compareTo(o1.getName());
}
}
//輸出結果
排序前---
g1 18 122
g2 17 244
g3 45 243
g4 9 67
g5 98 2
年齡排序後----
g4 9 67
g2 17 244
g1 18 122
g3 45 243
g5 98 2
人氣排序後----
g5 98 2
g4 9 67
g1 18 122
g3 45 243
g2 17 244
姓名升序排序後----
g1 18 122
g2 17 244
g3 45 243
g4 9 67
g5 98 2
姓名降序排序後----
g5 98 2
g4 9 67
g3 45 243
g2 17 244
g1 18 122
並非個人轉載於該地址:https://blog.csdn.net/jiangfeng861016/article/details/5632644
最後更新:2017-04-02 22:16:10