java lambda list过滤,使用Java 8 Lambda表达式过滤列表

该博客介绍了如何利用Java 8的Lambda表达式对项目列表进行过滤,根据指定年份筛选出匹配的项目,并获取这些项目的名称列表。通过stream()、filter()、flatMap()和collect()等方法,实现数据处理的简洁和高效。
摘要由CSDN通过智能技术生成

I have a Project class:

class Project {

List names;

int year;

public List getNames(){

return names;

}

}

Then I have another main function where I have a List and have to filter that list of projects on the basis of year and get names list as the result.

Can you please tell me how to do it using java 8 lambda expressions?

Thanks

解决方案

Well, you didn't state the exact filtering condition, but assuming you wish to filter elements by a given year:

List names = projects.stream()

.filter(p -> p.getYear() == someYear) // keep only projects of a

// given year

.flatMap(p -> p.getNames().stream()) // get a Stream of all the

// Names of all Projects

// that passed the filter

.collect(Collectors.toList()); // collect to a List

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值