设为首页 加入收藏

TOP

Java8 函数式编程探秘(九)
2018-01-30 12:42:56 】 浏览:914
Tags:Java8 函数 编程 探秘
BiFunction<Function<T,K>, BiFunction<S,T,R>, List<R>> join(List<T> destList, Map<K,S> srcMap) { return (dkeyFunc,mergeFunc) -> destList.stream().map( dest -> { K key = dkeyFunc.apply(dest); S src = srcMap.get(key); return mergeFunc.apply(src, dest); }).collect(Collectors.toList()); } /** 对给定的值 x,y 应用指定的二元操作函数 */ public static <T,S,R> Function<BiFunction<T,S,R>, R> op(T x, S y) { return opFunc -> opFunc.apply(x, y); } /** 将两个函数使用组合成一个函数,这个函数接受一个二元操作函数 */ public static <T,S,Q,R> Function<BiFunction<S,Q,R>, R> op(Function<T,S> funcx, Function<T,Q> funcy, T x) { return opFunc -> opFunc.apply(funcx.apply(x), funcy.apply(x)); } public static <T,S,Q,R> Function<BiFunction<S,Q,R>, Function<T,R>> op(Function<T,S> funcx, Function<T,Q> funcy) { return opFunc -> aT -> opFunc.apply(funcx.apply(aT), funcy.apply(aT)); } /** 将两个函数组合成一个叠加函数, compose(f,g) = f(g) */ public static <T> Function<T, T> compose(Function<T,T> funcx, Function<T,T> funcy) { return x -> funcx.apply(funcy.apply(x)); } /** 将若干个函数组合成一个叠加函数, compose(f1,f2,...fn) = f1(f2(...(fn))) */ public static <T> Function<T, T> compose(Function<T,T>... extraFuncs) { if (extraFuncs == null || extraFuncs.length == 0) { return x->x; } return x -> Arrays.stream(extraFuncs).reduce(y->y, FunctionUtil::compose).apply(x); } public static void main(String[] args) { System.out.println(multiGetResult( Arrays.asList( list -> list.stream().collect(Collectors.summarizingInt(x->x)), list -> list.stream().filter(x -> x < 50).sorted().collect(Collectors.toList()), list -> list.stream().collect(Collectors.groupingBy(x->(x%2==0? "even": "odd"))), list -> list.stream().sorted().collect(Collectors.toList()), list -> list.stream().sorted().map(Math::sqrt).collect(Collectors.toMap(x->x, y->Math.pow(2,y)))), Arrays.asList(64,49,25,16,9,4,1,81,36))); List<Integer> list = Arrays.asList(1,2,3,4,5); Supplier<Map<Integer,Integer>> mapSupplier = () -> list.stream().collect(Collectors.toMap(x->x, y-> y * y)); Map<Integer, Integer> mapValueAdd = list.stream().collect(Collectors.toMap(x->x, y->y, (v1,v2) -> v1+v2, mapSupplier)); System.out.println(mapValueAdd); List<Integer> nums = Arrays.asList(Arrays.asList(1,2,3), Arrays.asList(1,4,9), Arrays.asList(1,8,27)) .stream().flatMap(x -> x.stream()).collect(Collectors.toList()); System.out.println(nums); List<Integer> fibo = Arrays.asList(1,2,3,4,5,6,7,8,9,10).stream().collect(new FiboCollector()); System.out.println(fibo); System.out.println(op(new Integer(3), Integer.valueOf(3)).apply((x,y) -> x.equals(y.toString()))); System.out.println(op(x-> x.length(), y-> y+",world", "hello").apply((x,y) -> x+" " +y)); System.out.println(op(x-> x, y-> y+",world").apply((x,y) -> x+" " +y).apply("hello")); System.out.println(op(x-&g
首页 上一页 6 7 8 9 下一页 尾页 9/9/9
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇通向架构师的道路(第六天)之漫.. 下一篇JUnit 源码解析

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目