2021
09-04
09-04
js 数组 find,some,filter,reduce区别详解
区分清楚Array中filter、find、some、reduce这几个方法的区别,根据它们的使用场景更好的应用在日常编码中。Array.findArray.find返回一个对象(第一个满足条件的对象)后停止遍历constarrTest=[{id:1,name:"a"},{id:2,name:"b"},{id:3,name:"b"},{id:4,name:"c"}]//过滤条件functiongetName(val){returnarrTest=>arrTest.name===val}//如果我们是想找到第一个满足条件的数...
继续阅读 >
1.stream().reduce()单字段求和(1)普通数字求和publicstaticvoidtest2(){List<Integer>list=Arrays.asList(newInteger[]{1,2,3,4,5,6,7,8,9});Integersum=list.stream().reduce((x,y)->x+y).get();System.out.println(sum);}2.BigDecimal求和publicstaticvoidmain(String[]args){List<User>list=newArrayList<>();Useruser1=newUser();user1.setNum1(new...
reduce操作可以实现从Stream中生成一个值,其生成的值不是随意的,而是根据指定的计算模型。比如,之前提到count、min和max方法,因为常用而被纳入标准库中。事实上,这些方法都是reduce操作。reduce方法有三个override的方法:Optional<T>reduce(BinaryOperator<T>accumulator);Treduce(Tidentity,BinaryOperator<T>accumulator);<U>Ureduce(Uidentity,BiFunction<U,?superT,U>accumulator,BinaryOperator<U>...