栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Stream——获取集合中某个字段最前的值

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Stream——获取集合中某个字段最前的值

文章目录
  • 前言
  • 定义数据接收类
  • 定义数据集合
  • 数据筛选
    • 1、数据集合判空
      • 1.1、Optional.isPresent()
      • 1.2、orElse 替换
    • 2、排序 sorted
    • 3、数据过滤 filter
    • 4、获取第一个数据 findFirst().get()
  • 你以为这样就结束了?

前言

之前,针对Stream链式编程中的几个方法做了大致的说明。详情可以参考:

JDK 1.8 新特性之Stream 详解个人笔记

但实际业务中,总会存在很多复杂的思维,需要使用到Stream,此时玩的不熟练总感觉无从下手。

今后开始写几种常用的逻辑方式。

定义数据接收类

因为要考虑使用集合,需要使用到自定义类对象,所以先从定义对象开始。

public class User {
    private String name;
    private BigDecimal age;
    private Integer num;

    public User(String name, BigDecimal age) {
        this.name = name;
        this.age = age;
    }

    public User(String name, BigDecimal age, Integer num) {
        this.name = name;
        this.age = age;
        this.num = num;
    }
	// get/set
	// toString()
}
定义数据集合

自定义数据集合,模拟数据库查询到的数据信息。

List users = Arrays.asList(
                new User("xj1", new BigDecimal(20),3),
                new User("xj2",  new BigDecimal(3),5),
                new User("xj3",  new BigDecimal(30),1),
                new User("xj4",  new BigDecimal(10),4));
数据筛选

这里时模拟数据,采取手动定义,所以数据一定存在。

如果数据是从数据库中查询,此时这里的 users 集合 可能为 null

当出现null.stream()时,会出现报错信息!为了保证代码的健壮性,需要对数据信息进行判空。

1、数据集合判空

常见的stream判空有两种。

1.1、Optional.isPresent()
System.out.println("-----> " + Optional.ofNullable(null).isPresent());
System.out.println("-----> " + Optional.ofNullable(new ArrayList<>()).isPresent());

允许结果:

-----> false
-----> true

可以通过Optional.ofNullable(集合别名).isPresent()判断集合是否存在,再通过返回 boolean 判断是否继续向下执行链式编程代码。

1.2、orElse 替换

还有一种方式,是如果存在空对象,则将空对象进行替换操作。如下所示:

public class Test {
    public static void main(String[] args) {
        List users = Arrays.asList(
                new User("xj1", null),
                new User("xj2",  null),
                new User("xj3",  null),
                new User("xj4",  null));

        System.out.println(Optional.ofNullable(users).orElse(new ArrayList<>()));
        System.out.println(Optional.ofNullable(null).orElse(new ArrayList<>()));
    }
}


当Optional.ofNullable判断指定对象为null时,则会执行orElse(new ArrayList<>())进行替换,避免出现null.stream()等NPE问题。

2、排序 sorted

当将指定的集合转化为stream 流后,此时就需要针对数据流进行排序操作。可以采取sorted实现。

List users = Arrays.asList(
        new User("xj1", new BigDecimal(20),3),
        new User("xj2",  new BigDecimal(3),5),
        new User("xj3",  null,1),
        new User("xj4",  new BigDecimal(10),4));

Optional.ofNullable(users)
        .orElse(new ArrayList())
        .stream()
        .sorted(Comparator.comparing(User::getNum)) // 根据编号从小到大排序(默认)
        .forEach(e-> System.out.println(e)); // 只是为了打印流

3、数据过滤 filter

既然需要获取到指定的字段数据信息,如果明确要求不取为null的数据,此时则需要排序后的集合数据流进行数据的过滤,排除数据字段为null的数据信息。

List users = Arrays.asList(
                new User("xj1", new BigDecimal(20),3),
                new User("xj2",  new BigDecimal(3),5),
                new User("xj3",  null,1),
                new User("xj4",  new BigDecimal(10),4));
                
Optional.ofNullable(users)
                .orElse(new ArrayList())
                .stream()
                .sorted(Comparator.comparing(User::getNum))
                .filter(x->x.getAge()!=null)
                .forEach(e-> System.out.println(e)); // 只是为了打印流

4、获取第一个数据 findFirst().get()

通常来说,直接采取.findFirst().get(),一般是获取到集合中的 元 对象信息,如下所示:

List users = Arrays.asList(
                new User("xj1", new BigDecimal(20),3),
                new User("xj2",  new BigDecimal(3),5),
                new User("xj3",  null,1),
                new User("xj4",  new BigDecimal(10),4));
                
Optional.ofNullable(users)
                .orElse(new ArrayList())
                .stream()
                .sorted(Comparator.comparing(User::getNum))
                .filter(x->x.getAge()!=null)
                //.forEach(e-> System.out.println(e)); // 只是为了打印流
                .findFirst().get();
System.out.println(user);


如果数据类对象元素很多,只是想单单获取其中的某个列的值,则可以多加一个map进行映射。

获取指定元对象的指定属性值。

List users = Arrays.asList(
                new User("xj1", new BigDecimal(20),3),
                new User("xj2",  new BigDecimal(3),5),
                new User("xj3",  null,1),
                new User("xj4",  new BigDecimal(10),4));
BigDecimal bigDecimal = Optional.ofNullable(users)
                .orElse(new ArrayList())
                .stream()
                .sorted(Comparator.comparing(User::getNum))
                .filter(x -> x.getAge() != null)
                //.forEach(e-> System.out.println(e)); // 只是为了打印流
                .map(User::getAge)
                .findFirst()
                .get();
System.out.println(bigDecimal);

你以为这样就结束了?

你以为这样就结束了吗?接下来看一个极限栗子:

当你需要获取的列数据全是null,他会返回null吗?

List users = Arrays.asList(
                new User("xj1", null,3),
                new User("xj2",  null,5),
                new User("xj3",  null,1),
                new User("xj4",  null,4));
BigDecimal bigDecimal = Optional.ofNullable(users)
                .orElse(new ArrayList())
                .stream()
                .sorted(Comparator.comparing(User::getNum))
                .filter(x -> x.getAge() != null)
                //.forEach(e-> System.out.println(e)); // 只是为了打印流
                .map(User::getAge)
                .findFirst()
                .get();
System.out.println(bigDecimal);

执行一下,你就会发现新大陆:

解决这个问题,也很简单,把.get()换成.orElse(BigDecimal.ZERO)

List users = Arrays.asList(
                new User("xj1", null,3),
                new User("xj2",  null,5),
                new User("xj3",  null,1),
                new User("xj4",  null,4));
BigDecimal bigDecimal = Optional.ofNullable(users)
                .orElse(new ArrayList())
                .stream()
                .sorted(Comparator.comparing(User::getNum))
                .filter(x -> x.getAge() != null)
                //.forEach(e-> System.out.println(e)); // 只是为了打印流
                .map(User::getAge)
                .findFirst()
                //.get()
                .orElse(BigDecimal.ZERO);
System.out.println(bigDecimal);

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/975352.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号