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个技巧,希望对你有所帮助。