JDK1.8之stream lambda表达式

2/22/2017来源:ASP.NET技巧人气:1927

直接看代码

package com.jd.dock.web.controller.monitor;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;


public class TestInfo {


    /**
     * 测试使用串行流,并行流,以及使用普通遍历的时间消耗
     */
    public void parallelStream(){
        long t0 = System.nanoTime();
        Map<String,String> tests= Maps.newHashMap();
        for(int i=0;i<100;i++){
            tests.put("key:"+i,"value:"+i);
        }
        tests.keySet().forEach(key ->
                System.out.PRintln("key:"+key+"=value+"+tests.get(key)));
        /*tests.keySet().parallelStream().forEach(key ->
                System.out.println("key:"+key+"=value+"+tests.get(key)));*/
      /*  tests.keySet().stream().forEach(key ->
                System.out.println("key:"+key+"=value+"+tests.get(key)));*/
        long t1 = System.nanoTime();
        long millis  = TimeUnit.NANOSECONDS.toMillis(t1 - t0);
        System.out.println(String.format("it use time: %d ms", millis));
    }

    public void testCollage(){
        Set<String> skuIdSet =Sets.newHashSet("111","222","333","444");
        LinkedHashSet<String>  linktest = skuIdSet.stream().collect(LinkedHashSet::new,(ret,skuId)->ret.add(skuId+"test"),LinkedHashSet::addAll);
        System.out.println(linktest);
        linktest.removeIf(skuId -> skuId.equals("111test"));
        System.out.println(linktest);
        Map<String,String> testValue=Maps.newHashMap();
        testValue.put("aa","aaa");
        testValue.put("bb",null);
        System.out.println(testValue);
        testValue.values().removeIf(Objects::isNull);
        System.out.println(testValue);


    }

    public static void main(String args[]){
        TestInfo t =new TestInfo();
        t.testCollage();
    }

}

下面几篇博客讲得都不错

http://blog.csdn.net/u012702547/article/details/45420435 http://blog.csdn.net/kimylrong/article/details/47154879 http://www.cnblogs.com/WJ5888/p/4667086.html http://www.cnblogs.com/WJ5888/p/4618465.html http://www.cnblogs.com/imyijie/p/4478074.html https://segmentfault.com/a/1190000000377675?utm_source=tuicool http://blog.csdn.net/gdouchufu/article/details/29242429 http://blog.csdn.net/kimylrong/article/details/47665501 http://www.jianshu.com/p/5a49b10f3cfd http://www.liaoxuefeng.com/article/001411309538536a1455df20d284b81a7bfa2f91db0f223000 https://my.oschina.net/cloudcoder/blog/215169