java集合中对象排序
2010-05-29 11:29
411 查看
概述:本示例实现对象按年龄升序 人气升序排序功能 姓名升序 降序排序功能
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
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
相关文章推荐
- java集合对象属性排序
- Java自定义对象数组、集合排序
- 在Java中对集合当中的对象进行排序
- java中Comparator的用法(用于集合对象或数组对象进行排序)
- java集合中对象某属性比较排序Comp…
- java集合中对象某属性比较排序
- Java基础知识强化之集合框架笔记69:Collections类之ArrayList存储自自定义对象并排序的案例
- Java基础知识强化之集合框架笔记45:Set集合之TreeSet存储自定义对象并遍历练习1(自然排序)
- JAVA中根据集合中对象属性排序的方法
- java中对List集合对象做排序
- Java集合对象排序测试
- Java集合对象排序
- java中对List对象集合进行排序
- Java:List集合内的对象进行排序
- java 自定义对象集合排序
- 使用Comparable和Comparator对Java集合对象进行排序
- Java面试题,List集合中根据对象的某一属性排序
- 在Java中对集合当中的对象进行排序
- Java集合对象排序
- Java集合中对象排序