Java 8 Stream API features --- filter/reduce & predicate Interface test method
来源:互联网 发布:不用网络的赛车游戏 编辑:程序博客网 时间:2024/05/17 06:45
现在要对下面一组 number ,过滤掉被5整除的,再*2 .
jdk1.7 之前是这么做的:
public static void main(String... args){ List<Integer> values = Arrays.asList(11,22,33,44,55,60,75); int result = 0; for (int i : values) { if(i % 5 == 0){ result += i; } } System.out.println(result); }
如果我们以这种方式去实现的话,forEach 循环里的 code 我们把它称为 embedded recording ,意味着你要 focus on what to do and how to do,但是到了 jdk 1.8,使用 Stream API后,它的理念是不需要 focus on how to do , 只需要 focus on what to do .看下面的代码,而且上面的 code 还是属于 external iteration ,data 多的时候效率相比于下面的 internal ireration 要慢许多,这也是 Stream API 的一个优点。
System.out.println(values.stream() .filter(i -> i%5==0) .map(i -> i*2));看一下 filter method :
Returns a stream consisting of the elements of this stream that match the given predicate.
public interface Stream<T> extends BaseStream<T, Stream<T>> { /** * Returns a stream consisting of the elements of this stream that match * the given predicate. * * <p>This is an <a href="package-summary.html#StreamOps">intermediate * operation</a>. * * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, * <a href="package-summary.html#Statelessness">stateless</a> * predicate to apply to each element to determine if it * should be included * @return the new stream */ Stream<T> filter(Predicate<? super T> predicate);
对于 Predicate Interface : 它是一个 functional interface,只有一个 test method ,takes a parameter and return boolean.所以上面的 filter是这样实现的:
/** * Represents a predicate (boolean-valued function) of one argument. * * <p>This is a <a href="package-summary.html">functional interface</a> * whose functional method is {@link #test(Object)}. * * @param <T> the type of the input to the predicate * * @since 1.8 */@FunctionalInterfacepublic interface Predicate<T> { /** * Evaluates this predicate on the given argument. * * @param t the input argument * @return {@code true} if the input argument matches the predicate, * otherwise {@code false} */ boolean test(T t);
Predicate<Integer> p = new Predicate<Integer>() { @Override public boolean test(Integer i) { return i%5 == 0; }};
再用 lambda expression 把 p 替换掉就成了:
System.out.println(values.stream() .filter(i -> i%5==0) .map(i -> i*2) .reduce(Integer::sum));
所以从总体上来看,在使用 Stream API 时,实际上变成了函数式编程,没有 focus on Object,而是 focus on functions,.stream()、.filter()、.map()、.reduce() 四个 function ,像是不用再去 create object了。
更多请看这里:http://docs.oracle.com/javase/8/docs/api/
0 0
- Java 8 Stream API features --- filter/reduce & predicate Interface test method
- Java 8 Stream API features --- map/reduce methods
- Java 8: Stream filter method
- Java 8 Stream API Features Intermediate operations & Terminal operations
- Java 8: Stream map method
- java8 Stream API之reduce
- Java 8 Stream API
- Java 8 Stream API
- Python filter map reduce lambda Method detail
- Java 8 Stream API详解
- Java 8 Stream Filter 过滤null值
- Java 8系列之Stream中万能的reduce
- 【java】【java8】Lambda、Stream、Function、Consumer、Predicate、Supplier
- JAVA 8 Stream API 对Stream进行分组、分区
- Stream API:filter、map和flatMap方法
- Java 8新特性:Stream API
- Java 8新特性:Stream API
- Java 8新特性 Stream API 编程
- 1613-3-傅溥衍 总结《2016年12月14日》【连续第七十五天总结】
- 软件设计是怎样炼成的(8)——用户感觉好才是真的好(用户体验设计)
- 静态类 - static
- easonguo test
- sqlmap 的源码学习笔记一之目录结构
- Java 8 Stream API features --- filter/reduce & predicate Interface test method
- 实现字符串中寻找字串
- 如何更快速有效的收集Android应用的FPS
- 汉诺塔系列2
- oracle-where,group by,having order by 详细使用方法
- HTML常用标签属性(一)
- 集合框架_集合的遍历之迭代器遍历
- 纯CSS代码导航栏渐变下拉菜单
- EasyUI的下载与使用