Java 流式编程详解,Demo案例解析

Java 流式编程详解,Demo案例解析

Java Streams在很多年前就被引入了,但作为Java开发者,我们还没有完全掌握这个多功能工具的威力。在这里,你将发现一些有价值的技巧,可以作为参考并应用到你的下一个项目中。

在下面的示例中,我们将使用以下类。
在这里插入图片描述

@Getter
class Company {
  private String name;
  private Address address;
  private List personList;
}

@Getter
class Person {
  private Long id;
  private String name;
}

@Getter
class Address {
  private String street;
  private City city;
}

@Getter
class City {
  private String name;
  private State state;
}

@Getter
class State{
  private String name;
}

1.使用方法引用简化地图

以下代码可获取公司地址的城市名称。

public List getCityNames(List companyList){
  return companyList.stream()
    .map(company -> company.getAddress().getCity().getName())
    .toList();
}

可以替换为以下更具可读性的版本。

public List getCityNames(List companyList){
  return companyList.stream()
    .map(Company::getAddress)
    .map(Address::getCity)
    .map(City::getName)
    .toList();
}

2.空值检查

上述代码加上空值检查。

public List getCityNames(List companyList){
  return companyList.stream()
    .map(Company::getAddress)
    .filter(Objects::nonNull)
    .map(Address::getCity)
    .filter(Objects::nonNull)
    .map(City::getName)
    .filter(Objects::nonNull)
    .toList();
}

3.从流的流到流

以下代码获取所有公司的人员名单列表。

public List getAllPerson(List companyList){
  // 生成一个Person列表的列表
  List> partialResult = companyList.stream()
    .map(Company::getPersonList)
    .toList();

  // 将每个Person列表添加到结果中
  List result = new ArrayList<>();
  partialResult.forEach(result::addAll);

  return result;
}

可以用以下方式实现相同的功能。

public List getAllPerson(List companyList){
  return companyList.stream()
    .map(Company::getPersonList) // 返回一个Stream>
    .flatMap(List::stream)  // 返回一个Stream
    .toList(

4.按属性分组

以下代码将返回一张地图,其中包含每个城市的公司列表。

public Map> getCompaniesByCity(List companyList){
  return companyList.stream()
    .collect(Collectors.groupingBy(company -> company.getAddress().getCity()));
}

5.检查流中是否有项目

以下代码会检查是否有公司在某个城市。

public boolean hasCompanyInCity(List companyList, String cityName){
  return companyList.stream()
    .map(Company::getAddress)
    .map(Address::getName)
    .anyMatch(cityName::equals);
}

同样的方法也适用于noneMatch,如果你想检查某个城市是否有公司。

public boolean hasNoCompanyInCity(List companyList, String cityName){
  return companyList.stream()
    .map(Company::getAddress)
    .map(Address::getName)
    .noneMatch(cityName::equals);
}

6.记录日志

使用peek方法为每个返回的城市名记录日志。

public List getCityNames(List companyList){
  return companyList.stream()
    .map(Company::getAddress)
    .map(Address::getCity)
    .map(City::getName)
    .peek(cityName -> log.info(cityName))
    .toList();
}

7.获取唯一的城市名称

使用distinct从流中移除重复的城市名称。

public List getUniqueCityNames(List companyList){
  return companyList.stream()
    .map(Company::getAddress)
    .map(Address::getCity)
    .map(City::getName)
    .distinct()
    .toList();
}

以上就是通过实例展示的7个技巧,希望对你有所帮助。

来源:Java学研大本营

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值