关闭

List<T> 排序

371人阅读 评论(0) 收藏 举报
分类:

1、重载Collections.sort方法

package com.fang.dbtest;

public class User {
	private Integer id;
	private String name;
	private Integer age;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

}


package com.fang.dbtest;

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

public class Tests {
	public static void main(String[] arg) {
		List<User> list = new ArrayList<User>();
		User user1 = new User();
		User user2 = new User();
		User user3 = new User();
		user1.setId(1);
		user1.setName("name1");
		user1.setAge(3);
		user2.setId(2);
		user2.setName("name2");
		user2.setAge(2);
		user3.setId(3);
		user3.setName("name3");
		user3.setAge(1);
		list.add(user1);
		list.add(user2);
		list.add(user3);
		// 按照指定的属性升序排序
		Collections.sort(list, new Comparator<User>() {
			public int compare(User arg0, User arg1) {
				return arg0.getAge().compareTo(arg1.getAge());
			}
		});
		for (Iterator<User> it = list.iterator(); it.hasNext();) {
			User user = it.next();
			System.out.println(user.getName());
		}
	}

}

执行结果:

name3
name2
name1



2、就是list泛型中的对象实现Comparable接口:

package com.fang.dbtest;

public class User implements Comparable<User> {
	private Integer id;
	private String name;
	private Integer age;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
<pre name="code" class="java">        // 按照id 进行排序
	@Override
	public int compareTo(User o) {
		// TODO Auto-generated method stub
		return this.getId().compareTo(o.getId());
	}

}


package com.fang.dbtest;

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

public class Tests {
	public static void main(String[] arg) {
		List<User> list = new ArrayList<User>();
		User user1 = new User();
		User user2 = new User();
		User user3 = new User();
		user1.setId(1);
		user1.setName("name1");
		user1.setAge(3);
		user2.setId(2);
		user2.setName("name2");
		user2.setAge(2);
		user3.setId(3);
		user3.setName("name3");
		user3.setAge(1);
		list.add(user1);
		list.add(user2);
		list.add(user3);
		// 按照指定的属性升序排序
		Collections.sort(list);
		// 反转指定列表中元素的顺序。
		Collections.reverse(list);
		Iterator<User> it = list.iterator();
		while (it.hasNext()) {
			User user = it.next();
			System.out.println(user.getName());
		}
	}

}
执行结果:

name3
name2
name1



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:970497次
    • 积分:9691
    • 等级:
    • 排名:第1836名
    • 原创:253篇
    • 转载:78篇
    • 译文:0篇
    • 评论:93条
    文章分类
    最新评论