package js_method;
import java.util.List;
public interface CallBack {
T_INIT callBack(T_INIT initValue,T_ARRAY element,int index, List arr);
}
package js_method;
import java.util.List;
public abstract class Reduce implements CallBack{
@Override
public abstract INITVALUE_TYPE callBack(INITVALUE_TYPE initValue, ARRAY_TYPE element, int index, List arr);
public INITVALUE_TYPE reduce(List array,CallBack callBack,INITVALUE_TYPE initvalue) throws Exception {
if(array==null) throw new Exception("must have array!!!");
if(callBack==null) throw new Exception("must have callback function!!!");
int i=0;
if(initvalue == null){
while(i
package js_method;
import java.util.Arrays;
import java.util.List;
public class UseReduce {
public static void main(String[] args) throws Exception {
Reduce reduce = new Reduce() {
@Override
public Integer callBack(Integer initValue, Integer element, int index, List arr) {
return initValue+element;
}
};
Integer reduceRes = reduce.reduce(Arrays.asList(1, 2, 3, 4, 5), reduce, null);
System.out.println(reduceRes);
}
}