java对象排序

 

集合和数组的排序

示例代码如下:

StudentCompare 类实现了Comparable和Comparator两个接口

package com.compare.bean;

import java.util.Comparator;
/**
* @version 1.0
* @author HUAZAI
* @Date 2014-03-05
*/
public class StudentCompare implements Comparable<StudentCompare>,
Comparator<StudentCompare> {
private String name;
private int age;
private float score;

/**
* 实现Comparable接口的方法
*/
public int compareTo(StudentCompare student) {
if (this.score > student.score) {
return -1;
} else if (this.score < student.score) {
return 1;
} else {
if (this.age > student.age) {
return 1;
} else if (this.age < student.age) {
return -1;
} else {
return 0;
}
}
}

/**
* 实现Comparator接口的方法
*/
public int compare(StudentCompare o1, StudentCompare o2) {
if (o1.score > o2.score) {
return 1;
} else if (o1.score < o2.score) {
return -1;
} else {
if (o1.age > o2.age) {
return 1;
} else if (o1.age < o2.age) {
return -1;
} else {
return 0;
}
}
}

/**
* 带有完全参数的构造函数
*
* @param name
* @param age
* @param score
*/
public StudentCompare(String name, int age, float score) {
this.name = name;
this.age = age;
this.score = score;
}

/**
* 没有参数的构造函数
*
* @param name
* @param age
* @param score
*/
public StudentCompare() {

}

/**
* 对象格式化
*/
public String toString() {
return name + "\t" + age + "\t" + score;
}

// get/set方法
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 float getScore() {
return score;
}

public void setScore(float score) {
this.score = score;
}
}

 

测试类的代码如下:

package com.compare.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.compare.bean.StudentCompare;

public class CompareTest {
/**
* 生成对象数组
*
* @return StudentCompare[]
*/
public static StudentCompare[] createArray() {
StudentCompare[] stu = { new StudentCompare("Java", 22, 98),
new StudentCompare("张三", 22, 93),
new StudentCompare("李四", 20, 93),
new StudentCompare("王五", 22, 100),
new StudentCompare("马六", 21, 77),
new StudentCompare("龙七", 53, (float) 69.5) };
return stu;
}

/**
* 生成对象list
*
* @return List<StudentCompare>
*/
public static List<StudentCompare> createList() {
List<StudentCompare> list = new ArrayList<StudentCompare>();
list.add(new StudentCompare("Java", 22, 98));
list.add(new StudentCompare("张三", 22, 93));
list.add(new StudentCompare("李四", 20, 93));
list.add(new StudentCompare("王五", 22, 100));
list.add(new StudentCompare("马六", 21, 77));
list.add(new StudentCompare("龙七", 53, (float) 69.5));
return list;
}

/**
* 对象数组转化成对象list
*
* @param stu
* @return
*/
public static List<StudentCompare> arrayToList(StudentCompare[] stu) {
List<StudentCompare> list = new ArrayList<StudentCompare>();
for (int i = 0; i < stu.length; i++) {
list.add(stu[i]);
}
return list;
}

/**
* 对象list转化成对象数组
*
* @param list
* @return
*/
public static StudentCompare[] listToArray(List<StudentCompare> list) {
StudentCompare[] stu = createArray();
for (int i = 0; i < list.size(); i++) {
stu[i] = list.get(i);
}
return stu;
}

/**
* 用List排序
*
* @param list
*/
public static void sortWithList(List<StudentCompare> list) {
System.out.println("升序排列元素:");
// Collections.sort(list); //实现Comparable接口
Collections.sort(list, new StudentCompare()); // 实现Comparator接口,根据自定义比较器比较

// System.out.println("降序排列元素:");
// Collections.sort(list, Collections.reverseOrder());
// //实现Comparator接口,根据collections产生的比较器

// System.out.println("Collections.reverse 从列表中最后一个元素开始输出:");
Collections.reverse(list);

for (int i = 0; i < list.size(); i++) {
StudentCompare stucompare = list.get(i);
System.out.println(stucompare);
}

}

/**
* 用Array排序
*
* @param stu
*/
public static void sortWithArray(StudentCompare[] stu) {
// Arrays.sort(stu);实现Comparable接口
Arrays.sort(stu, new StudentCompare());// 实现Comparator接口,根据自定义比较器比较

// System.out.println(Arrays.asList(stu));
for (int i = 0; i < 6; i++) {
System.out.println(stu[i]);
}
}

public static void main(String[] args) {
sortWithList(createList());
// sortWithArray(createArray());
}

}

转载于:https://www.cnblogs.com/XiaoyangBoke/p/3582064.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值