草庐IT

foldleft

全部标签

foldleft参数签名:o

typeSet=Int=>Booleandefcontains(s:Set,elem:Int):Boolean=s(elem)defunion(s:Set,t:Set):Set=(x=>contains(t,x)||contains(s,x))defsingletonSet(elem:Int):Set=(x=>x==elem)...vals1=singletonSet(1)vals2=singletonSet(2)vals3=singletonSet(3)//worksvals=List(s1,s2,s3).foldLeft(union(_,_))//errorvals=List(s1,s2,

java - 等效于 Java 8 中的 Scala 的 foldLeft

什么相当于Java8中Scala伟大的foldLeft?我很想它是reduce,但reduce必须返回与它所归约的类型相同的东西。例子:importjava.util.List;publicclassFoo{//thismethodworksprettywellpublicintsum(Listnumbers){returnnumbers.stream().reduce(0,(acc,n)->(acc+n));}//thismethodmakesthefilenotcompilepublicStringconcatenate(Listchars){returnchars.stream(