java8返回多个值,Java 8流加入并返回多个值

I'm porting a piece of code from .NET to Java and stumbled upon a scenario where I want to use stream to map & reduce.

class Content

{

private String propA, propB, propC;

Content(String a, String b, String c)

{

propA = a; propB = b; propC = c;

}

public String getA() { return propA; }

public String getB() { return propB; }

public String getC() { return propC; }

}

List contentList = new ArrayList();

contentList.add(new Content("A1", "B1", "C1"));

contentList.add(new Content("A2", "B2", "C2"));

contentList.add(new Content("A3", "B3", "C3"));

I want to write a function that can stream through the contents of contentlist and return a class with result

content { propA = "A1, A2, A3", propB = "B1, B2, B3", propC = "C1, C2, C3" }

I'm fairly new to Java so you might find some code that resembles more like C# than java

解决方案

You can use proper lambda for BinaryOperator in reduce function.

Content c = contentList

.stream()

.reduce((t, u) -> new Content(

t.getA() + ',' + u.getA(),

t.getB() + ',' + u.getB(),

t.getC() + ',' + u.getC())

).get();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值