Java Collections反转与排序

1、反转(reverse)

前后反转。如下:
public static void main(String args[]) {
        List<Long> num = Lists.newArrayList();
        num.add(3L);
        num.add(5L);
        num.add(1l);
        num.add(0l);
        System.out.println("正常" + num);
        Collections.reverse(num);
        System.out.println("反转" + num);
    }

运行结果如下:

正常[3, 5, 1, 0]
反转[0, 1, 5, 3]

2、排序(sort)

从下到大排序。如下:

public static void main(String args[]) {
        List<Double> num = Lists.newArrayList();
        num.add(333.0);
        num.add(555.0);
        num.add(111.0);
        num.add(1.0);
        System.out.println("正常" + num);
        Collections.sort(num);
        System.out.println("排序" + num);
    }

运行结果如下:

正常[333.0, 555.0, 111.0, 1.0]
排序[1.0, 111.0, 333.0, 555.0]

3、反转与排序结合

从大到小排序。如下:

public static void main(String args[]) {
        List<Double> num = Lists.newArrayList();
        num.add(333.0);
        num.add(555.0);
        num.add(111.0);
        num.add(1.0);
        System.out.println("正常" + num);
        Collections.sort(num);
        System.out.println("排序" + num);
        Collections.reverse(num);
        System.out.println("反转排序" + num);
    }

运行结果如下:

正常[333.0, 555.0, 111.0, 1.0]
排序[1.0, 111.0, 333.0, 555.0]
反转排序[555.0, 333.0, 111.0, 1.0]

4、对Object对象排序反转

排序对象需要实现Comparable接口,并重写compareTo()方法。 如下:

public class TestList implements Comparable<TestList>{

    private int score;

    private int age;

    public TestList(int score, int age){
        super();
        this.score = score;
        this.age = age;
    }

    public int getScore() {
        return score;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public int compareTo(TestList o) {
        int i = this.getAge() - o.getAge();//先按照age排序
        if(i == 0){
            return this.score - o.getScore();//如果age相等了再用score进行排序
        }
        return i;
    }

    public static void main(String args[]) {
        List<TestList> lists = Lists.newArrayList();
        lists.add(new TestList(78, 26));
        lists.add(new TestList(67, 23));
        lists.add(new TestList(34, 56));
        lists.add(new TestList(55, 23));
        System.out.println("排序前:");
        for(TestList list : lists){
            System.out.println(list.getAge() + "," + list.getScore());
        }
        Collections.sort(lists);
        System.out.println("排序后:");
        for(TestList list : lists){
            System.out.println(list.getAge() + "," + list.getScore());
        }
        Collections.reverse(lists);
        System.out.println("反转后:");
        for(TestList list : lists){
            System.out.println(list.getAge() + "," + list.getScore());
        }
    }

}

运行结果如下:

排序前:
16,38
13,17
26,14
13,25
排序后:
13,17
13,25
16,38
26,14
反转后:
26,14
16,38
13,25
13,17

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值