客户化排序List集合

在使用List集合时,通常情况下希望从集合中得到的对象是按照一定顺序排列的,但是List集合的默认排序方式为按照对象的插入排序,可以通过java.util.Collections类的静态方法sort(List<T> list),sort(List<T>list,Comparator<?super T >c)或reverse(List<?> list)对集合中的对象进行客户化排序,其中方法sort(List<T> list)、reverse(List<?> list)要求集合中的元素必须实现java.lang.Comparable接口,即实现方法compareTo(),该方法的具体定义为:

public int compareTo(T o);

方法sort(List<T> list)是将集合中所有的对象按正序排列,而方法reverse(List<?> list)是将集合中的所有对象按照倒序排列,方法sort(List<T>list,Comparator<?super T >c)不要求集合中的对象必须实现Comparable接口,但是在使用该方法时需要显示设置比较器,即该方法的第二个入口参数,比较器必须实现java.util.Comparator接口,即实现方法compare()  ,该方法的具体定义为:

int compare(T 01 ,T 02);

当然,java.util.Arrays类的静态方法sortt(int[] a)也可以进行客户化排序,对于Collections.sort和Arrays.的sort(List<T> list)的区别为:Collection.sort是给List<T>进行排序,而Arrays.sort是给数组进行排序。

比较器的的功能是实现对集合中所有对象的排序策略,下面通过例子做具体解析。

实现Comparable接口

import java.util.Arrays;
import java.util.Collections;

public class Person implements Comparable<Person>
{
	String name;
	int age;
	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 Person(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	
	public int compareTo(Person obj)
	{
		System.out.println("调用compareTo方法");
		int i=0;
		i = name.compareTo(obj.name);	//字符串的compareTo比较方法
		if(i == 0)
		{
			return this.age - obj.age;
		}
		else
		{
			return i;
		}
	}
	
	public String toString()
	{
		return (this.name+" "+this.age);
	}
	
	public static void main(String...strings)
	{
		Person[] ps = new Person[3];
		ps[0]  = new Person("Tom",20);
		ps[1] = new Person("Mike", 18);
		ps[2] = new Person("Mike",20);
		System.out.println(Arrays.asList(ps));
		//Arrays.sort()的排序对象为数组
		Arrays.sort(ps);
		//Collections.sort的排序对象为List<T o>
	//	Collections.sort(Arrays.asList(ps));
		System.out.println(Arrays.asList(ps));
	}
}

实现Comparator接口

public class Person2 {
	private String name;
	private int age;
	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 Person2(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
}


import java.util.Arrays;
import java.util.Comparator;

public class Person2Comparator implements Comparator<Person2> 
{
	public int compare(Person2 one ,Person2 another)
	{
		int i=0;
		i = one.getName().compareTo(another.getName());
		if(i == 0)
		{
			return one.getAge()-another.getAge();
		}else
		{
			return i;
		}
	}
	
	public static void main(String[] args)
	{
		Person2[] ps = new Person2[3];
		ps[0]  = new Person2("Tom",20);
		ps[1] = new Person2("Mike", 18);
		ps[2] = new Person2("Mike",20);
		
		Arrays.sort(ps,new Person2Comparator());
		for(Person2 p : ps)
		{
			System.out.println(p.getName()+","+p.getAge());
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只学弱狗!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值