Java8 用Lambda表达式给List集合排序的实现

 更新时间:2019年08月06日 11:30:12   作者:寻找风口的猪  
这篇文章主要介绍了Java8 用Lambda表达式给List集合排序的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

Lambda用到了JDK8自带的一个函数式接口Comparator<T>。

准备一个Apple类

public class Apple {
  private int weight;
  private String color;

  public Apple(){}

  public Apple(int weight) {
    this.weight = weight;
  }

  public Apple(int weight, String color) {
    this.weight = weight;
    this.color = color;
  }
  
  setters();getters();toString(); 
}

步骤一:

public class AppleComparator implements Comparator<Apple> {
  @Override
  public int compare(Apple o1, Apple o2) {
    return o1.getWeight() - o2.getWeight();
  }
}

步骤二:准备一个List集合

ArrayList<Apple> inventory = Lists.newArrayList(
        new Apple(10, "red"),
        new Apple(5, "red"),
        new Apple(1, "green"),
        new Apple(15, "green"),
        new Apple(2, "red"));

步骤三:顺序排序,三种方式

/**
 * 顺序排序
 */
// 1、传递代码,函数式编程
inventory.sort(new AppleComparator());
System.out.println(inventory);

// 2、匿名内部类
inventory.sort(new Comparator<Apple>() {
  @Override
  public int compare(Apple o1, Apple o2) {
    return o1.getWeight() - o2.getWeight();
  }
});

// 3、使用Lambda表达式
inventory.sort((a, b) -> a.getWeight() - b.getWeight());

// 4、使用Comparator的comparing
Comparator<Apple> comparing = comparing((Apple a) -> a.getWeight());
inventory.sort(comparing((Apple a) -> a.getWeight()));
//或者等价于
inventory.sort(comparing(Apple::getWeight));

步骤四:逆序排序

/**
 * 逆序排序
 */
// 1、 根据重量逆序排序
inventory.sort(comparing(Apple::getWeight).reversed()); 

步骤五:如果两个苹果一样重,就得再找一个条件来进行排序

// 2、如果两个苹果的重量一样重,怎么办?那就再找一个条件进行排序呗
inventory.sort(comparing(Apple::getWeight).reversed().thenComparing(Apple::getColor));

https://gitee.com/play-happy/base-project

参考:

【1】《Java8实战

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Apache DolphinScheduler完全设置东八区时区

    Apache DolphinScheduler完全设置东八区时区

    这篇文章主要为大家介绍了Apache DolphinScheduler完全设置东八区配置详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • 用java实现跳动的小球示例代码

    用java实现跳动的小球示例代码

    这篇文章主要介绍了用java实现跳动的小球,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • spring boot在启动项目之后执行的实现方法

    spring boot在启动项目之后执行的实现方法

    在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,下面这篇文章就来给大家介绍了关于spring boot在启动项目之后执行自己要执行的东西的实现方法,文中给出了详细的示例代码,需要的朋友可以参考下。
    2017-09-09
  • spring boot整合mybatis+mybatis-plus的示例代码

    spring boot整合mybatis+mybatis-plus的示例代码

    这篇文章主要介绍了spring boot整合mybatis+mybatis-plus的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • MyBatis配置的应用与对比jdbc的优势

    MyBatis配置的应用与对比jdbc的优势

    这篇文章主要介绍了MyBatis配置的使用与相对于jdbc的优势,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • mybatis中的if-else及if嵌套使用方式

    mybatis中的if-else及if嵌套使用方式

    这篇文章主要介绍了mybatis中的if-else及if嵌套使用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 详解Spring Boot使用系统参数表提升系统的灵活性

    详解Spring Boot使用系统参数表提升系统的灵活性

    Spring Boot项目中常有一些相对稳定的参数设置项,其作用范围是系统级的或模块级的,这些参数称为系统参数。这些变量以参数形式进行配置,从而提高变动和扩展的灵活性,保持代码的稳定性
    2021-06-06
  • 在非spring环境中调用service中的方法

    在非spring环境中调用service中的方法

    非Spring环境指的是不使用Spring框架来管理和配置应用程序的运行时环境,本文将给大家介绍如何在非spring环境中调用service中的方法,文中有详细实现步骤,需要的朋友可以参考下
    2024-03-03
  • eclipse+myeclipse 环境配置方法

    eclipse+myeclipse 环境配置方法

    eclipse+myeclipse配置环境
    2009-07-07
  • springboot手动动态注入controller和service方式

    springboot手动动态注入controller和service方式

    这篇文章主要介绍了springboot手动动态注入controller和service方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03

最新评论