首页 > 其他 > 详细

比较器复合

时间:2018-09-02 23:13:02      阅读:211      评论:0      收藏:0      [点我收藏+]

1. 逆序:

package com.ant.jdk8.chap03;

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

public class ComparatorCompositeDemo {
    public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(new Apple("green",2,"China"),
                new Apple("green",1,"Japan"),
                new Apple("red",3,"America"));
        apples.sort(Comparator.comparing(Apple::getWeight).reversed());
        apples.stream().forEach(apple-> System.out.println(apple.getWeight()));
    }
}

技术分享图片

2. 比较器链:

package com.ant.jdk8.chap03;

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

public class ComparatorCompositeDemo {
    public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(new Apple("green",2,"China"),
                new Apple("green",2,"Japan"),
                new Apple("red",3,"America"));
        apples.sort(Comparator.comparing(Apple::getWeight).reversed().thenComparing(Apple::getCountry));
        apples.stream().forEach(apple-> System.out.println(apple.getWeight()+"->"+apple.getCountry()));
    }
}

技术分享图片

 

比较器复合

原文:https://www.cnblogs.com/i-hard-working/p/9575660.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!