import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
AdList<Integer> adl = new AdList();
adl.add(1);
adl.add(2);
adl.add(3);
adl.add(4);
adl.add(5);
adl.add(6);
AdList<Integer> adl2 = adl.map(i -> i*10);
AdList<Integer> adl3 = adl.filter(i -> i%2 ==0);
int adl4 = adl.reduce((a,b) -> a+b);
AdList<String> lines = new AdList<>();
lines.add("spark hadoop flink");
lines.add("spark flink flume");
lines.add("hive flink flume");
AdList<String> adl5 = lines.filtMap(i -> Arrays.asList(i.split(" ")));
System.out.println(adl5);
}
}
import java.util.ArrayList;
import java.util.Collection;
public class AdList<T> extends ArrayList<T> {
public <B> AdList<B> map(MapFunction<T,B> fun) {
AdList<B> nbs = new AdList<>();
for (T t : this) {
B b = fun.apply(t);
nbs.add(b);
}
return nbs;
}
public AdList<T> filter(FilterFunction<T,Boolean> fun) {
AdList<T> nts = new AdList<>();
for (T t : this) {
if (fun.apply(t)){
nts.add(t);
}
}
return nts;
}
public T reduce(ReduceFunction<T,T,T> fun) {
T acc =null;
Boolean flag =true;
for (T t : this) {
if (flag){
acc = t;
flag = false;
}else {
acc = fun.apply(acc, t);
}
}
return acc;
}
public <B> AdList<B> filtMap(FlatMapFunction<T, Collection<B>> fun) {
AdList<B> nbs = new AdList<>();
for (T t : this) {
Collection<B> apply = fun.apply(t);
nbs.addAll(apply);
}
return nbs;
}
}
public interface MapFunction<A,B> {
B apply (A a);
}
public interface FilterFunction<A,Boolean> {
Boolean apply (A a );
}
public interface ReduceFunction<A,B,C> {
C apply (A a,B b);
}
public interface FlatMapFunction<A,B> {
B apply (A a);
}