Java 8 Stream API features --- filter/reduce & predicate Interface test method

现在要对下面一组 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
 */
@FunctionalInterface
public 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值