Java 流式编程的七个必学技巧

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

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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

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

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

public List getCityNames(List companyList){
  return companyList.stream()
    .map(company -> company.getAddress().getCity().getName())
    .toList();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

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

public List getCityNames(List companyList){
  return companyList.stream()
    .map(Company::getAddress)
    .map(Address::getCity)
    .map(City::getName)
    .toList();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

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();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

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;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

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

public List getAllPerson(List companyList){
  return companyList.stream()
    .map(Company::getPersonList) // 返回一个Stream>
    .flatMap(List::stream)  // 返回一个Stream
    .toList(
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

4. 按属性分组

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

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

5. 检查流中是否有项目

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

public boolean hasCompanyInCity(List companyList, String cityName){
  return companyList.stream()
    .map(Company::getAddress)
    .map(Address::getName)
    .anyMatch(cityName::equals);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

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

public boolean hasNoCompanyInCity(List companyList, String cityName){
  return companyList.stream()
    .map(Company::getAddress)
    .map(Address::getName)
    .noneMatch(cityName::equals);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

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();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

7. 获取唯一的城市名称

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

public List getUniqueCityNames(List companyList){
  return companyList.stream()
    .map(Company::getAddress)
    .map(Address::getCity)
    .map(City::getName)
    .distinct()
    .toList();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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

责任编辑:赵宁宁 来源: Java学研大本营
相关推荐

2015-06-11 13:34:54

编程编程阶段

2023-09-07 16:28:46

JavaScrip

2021-11-22 12:13:54

Linuxwget 命令

2022-11-21 17:58:23

编程语言技巧

2021-08-17 10:08:44

HTML网站网络

2022-04-14 10:40:11

领导者IT团队远程团队

2023-05-30 09:59:38

2018-05-24 08:47:15

数据存储技巧

2016-12-13 10:06:25

编写Java单元测试技巧

2019-09-09 10:32:51

基于意图的网络IBN网络

2024-11-08 16:24:39

2022-07-14 10:34:13

IT领导者CIO首席信息官

2021-06-28 11:46:31

GitLinux

2018-04-27 09:22:21

数据存储技巧

2025-03-21 08:20:00

数据清洗Python编程

2015-11-30 17:12:31

Git使用技巧

2022-08-26 08:00:00

数字时代IT首席信息官

2012-09-17 10:57:39

邮件安全

2023-04-19 15:29:53

通信技巧Vue 3开发

2023-12-15 08:51:48

点赞
收藏

51CTO技术栈公众号