java lisi乱序、排序

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

package com.gcj.test;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;



/**
 * 
 * 排序Test
 * @author youps-a
 * @version $Id: Test.java, v 0.1 2015年5月22日 下午8:04:45 youps-a Exp $
 */
public class Test {

	@SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) throws ParseException {
	    /**排序方法简单使用*/
        List list = Arrays.asList(1,2,3,4,5,6,7,8,9);   //定义个list
        System.out.println("原  顺  序"+list.toString());
        
        Collections.shuffle(list);  
        System.out.println("打乱顺序"+list.toString());
        
        Collections.sort(list);
        System.out.println("正序排列"+list.toString());
        
        Collections.sort(list, Collections.reverseOrder());
        System.out.println("逆序排列"+list.toString());
        
        /**排序方法复杂使用*/
        List<TestBean> testList = new ArrayList<TestBean>();
        for (int i = 0; i < 10; i++) {
            TestBean testBean = new TestBean();
            testBean.setId(i);
            testBean.setName("name"+i);
            testList.add(testBean);
        }
        //定义一个TestBean 类型的list 并打乱进行测试
        Collections.shuffle(testList);  
        for (int i = 0; i < testList.size(); i++) {
            System.out.println("testList乱序  "+testList.get(i).toString());
        }
        System.out.println();
        //根据TestBean 的 id 属性进行排序,需重载实现
        Collections.sort(testList, new Comparator<TestBean>() {
            @Override
            public int compare(TestBean o1, TestBean o2) {
                // 实现正序排列
                return o1.getId().compareTo(o2.getId());
            }
        });
        for (int i = 0; i < testList.size(); i++) {
            System.out.println("testList正序  "+testList.get(i).toString());
        }
        System.out.println();
        //再次打乱顺序
        Collections.shuffle(testList);  
        //依旧重载方法
        Collections.sort(testList, new Comparator<TestBean>() {
            @Override
            public int compare(TestBean o1, TestBean o2) {
                // 实现倒序排列
                return o2.getId().compareTo(o1.getId());
            }
        });
        for (int i = 0; i < testList.size(); i++) {
            System.out.println("testList倒序  "+testList.get(i).toString());
        }

	}
	
}
//测试bean
class TestBean {
    //注意需使用 Integer 或Long 等包装类型
    private Integer id;
    private String name;
    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;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "id:"+this.getId()+",name:"+this.getName();
    }
}