您的位置:首页 > 编程语言 > Java开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息