`
85977328
  • 浏览: 1873003 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java中List的排序功能的实现

阅读更多
    今天在工作的时候,遇到了List排序的问题,所以总结了一下,与大家分享.Collections.sort排序的时候,用到了Comparator接口下面的compare()方法.下面的小例子中,还用到了匿名类技术和泛型,若朋友们看不懂,可以留言提问^-^
    compare(Object 对象1,Object 对象2)重写时
1) 若返回负数,则表示 对象1<对象2
2) 若返回0,则表示 对象1=对象2
3) 若返回正数,则表示 对象1>对象2
    反过来,在String类中,他有个CompareTo()方法,他的比较结果也是如此
    字符串1.CompareTo(字符串2)比较结果
1) 若字符串1<字符串2,则返回负数
2) 若字符串1=字符串2,则返回0
3) 若字符串1>字符串2,则返回正数

//SortList.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class SortList {
	public static void main(String[] args) {
		// 方法 一 Test
		// 测试数据
		Test t1 = new Test(88, "phl");
		Test t2 = new Test(6, "aaa");
		Test t3 = new Test(3, "abc");
		Test t4 = new Test(5, "aac");
		Test t5 = new Test(4, "adc");
		Test t6 = new Test(4, "aac");
		Test t7 = new Test(4, "aaa");

		List<Test> ts = new ArrayList<Test>();
		ts.add(t1);
		ts.add(t2);
		ts.add(t3);
		ts.add(t4);
		ts.add(t5);
		ts.add(t6);
		ts.add(t7);
		// 排序,通过泛型和匿名类来实现
		Collections.sort(ts, new Comparator<Test>() {

			public int compare(Test o1, Test o2) {
				int result = o1.getId() - o2.getId();
				if (result == 0) {
					result = o1.getName().compareTo(o2.getName());
				}
				return result;
			}
		});
		// 打印排序结果
		Iterator<Test> iterator = ts.iterator();
		while (iterator.hasNext()) {
			Test test = iterator.next();
			System.out.println("id=" + test.getId() + ";name=" + test.getName());
		}

		System.out.println("*******************************************");
		// 方法 二 Test2
		// 测试数据
		//Comparable<Test2>[] ts2 = new Test2[5];
		Test2[] ts2 = new Test2[5];
		ts2[0] = new Test2(11, "phl");
		ts2[1] = new Test2(16, "aaa");
		ts2[2] = new Test2(21, "adc");
		ts2[3] = new Test2(14, "acd");
		ts2[4] = new Test2(6, "ada");
		java.util.Arrays.sort(ts2);

		// 打印排序结果
		for (int i = 0; i < ts2.length; i++) {
			System.out.println("id=" + ts2[i].getId() + ";name=" + ts2[i].getName());
		}
	}
}

// 测试类
class Test {
	private int id;
	private String name;

	public Test(int id, String name) {
		this.id = id;
		this.name = name;
	}

	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;
	}
}

// 测试类
class Test2 implements java.lang.Comparable<Test2> {
	private int id;
	private String name;

	public Test2(int id, String name) {
		this.id = id;
		this.name = name;
	}

	// 实现接口比较方法
	public int compareTo(Test2 t) {
		return this.id - t.getId();
	}

	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;
	}
}

/*
输出结果如下
id=3;name=abc
id=4;name=aaa
id=4;name=aac
id=4;name=adc
id=5;name=aac
id=6;name=aaa
id=88;name=phl
*******************************************
id=6;name=ada
id=11;name=phl
id=14;name=acd
id=16;name=aaa
id=21;name=adc
 */
1
3
分享到:
评论
3 楼 杀手请杀人 2014-03-12  
别听他说用java.lang.Comparable好 其实这样反而破坏对象的封装 你想想也行你还有其他队形需要比较 是不是又要在那个对象里面重写了呢 这样是多余的 而将比较器和对象分离更加合适 我可以拿比较器来比较任何对象   而且你在使用Comparable时候有时候必须得重写object中的equals方法还有hashcode方法 我觉得Comparator更加好说实话
2 楼 85977328 2010-06-22  
mercyblitz 写道
让Test实现java.lang.Comparable接口更好。

恩,确实接口实现的好一些
只不过没办法使用List的相关强大接口了,好像只能通过数组来操作了
谢谢你的意见,多多交流和沟通
1 楼 mercyblitz 2010-06-22  
让Test实现java.lang.Comparable接口更好。

相关推荐

Global site tag (gtag.js) - Google Analytics