码迷,mamicode.com
首页 > 其他好文 > 详细

比较器复合

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

标签:java   ack   etc   com   ica   .so   try   int   比较   

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()));
    }
}

技术分享图片

 

比较器复合

标签:java   ack   etc   com   ica   .so   try   int   比较   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!