java8 stream

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;


/**
 * Created by shaozhiqi on 2019/9/26
 */
public class testLam {


    @Test
    public void testCreateThread() {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("用匿名内部类的方式来创建线程");
            }
        });

        Thread thread2 = new Thread(() -> {
            System.out.println("使用Lambda来创建线程");
        });

        thread1.start();
        thread2.start();

        //执行结果:
        //用匿名内部类的方式来创建线程
        //使用Lambda来创建线程

    }

    //stream() − 串行流处理
    //parallelStream() - 并行流处理
    @Test
    public void testSteam() {
        Map map1 = new HashMap<String, String>();
        map1.put("age", 20);
        map1.put("name", "shaozhiqi");
        map1.put("company", "A公司");

        Map map2 = new HashMap<String, String>();
        map2.put("age", 21);
        map2.put("name", "qqq");
        map2.put("company", "B公司");

        Map map3 = new HashMap<String, String>();
        map3.put("age", 19);
        map3.put("name", "bbb");
        map3.put("company", "B公司");

        Map map4 = new HashMap<String, String>();
        map4.put("age", 25);
        map4.put("name", "LLL");
        map4.put("company", "C公司");

        List<Map> lsM = new ArrayList<>();
        lsM.add(map1);
        lsM.add(map2);
        lsM.add(map3);
        lsM.add(map4);

        //年龄过滤-------------------------------------------------------------------------------------------------------
        List<Map> ls1 = lsM.stream().filter(m -> Integer.valueOf(m.get("age").toString()) > 20).collect(Collectors.toList());
        ls1.forEach(map -> System.out.println(map));
        //结果:
        //{name=qqq, company=B公司, age=21}
        //{name=LLL, company=C公司, age=25}

        //收集所有人姓名-------------------------------------------------------------------------------------------------------
        List<String> ls2 = lsM.stream().map(m -> m.get("name").toString()).collect(Collectors.toList());
        ls2.forEach(name -> System.out.println(name));
        //结果:
        //shaozhiqi
        //qqq
        //bbb
        //LLL

        //数据去重------------------------------------------------------------------------------------------------------
        Map map5 = new HashMap<String, String>();
        map5.put("age", 25);
        map5.put("name", "LLL");
        map5.put("company", "C公司");
        lsM.add(map5);
        List<Map> ls3 = lsM.stream().distinct().collect(Collectors.toList());
        ls3.forEach(map -> System.out.println(map));
        //结果:
        //{name=shaozhiqi, company=A公司, age=20}
        //{name=qqq, company=B公司, age=21}
        //{name=bbb, company=B公司, age=19}
        //{name=LLL, company=C公司, age=25}


        //排序----------------------------------------------------------------------------------------------------------
       /* List<Map> ls4 = lsM.stream().sorted(new Comparator<Map>() {
            @Override
            public int compare(Map o1, Map o2) {
                return o1.get("age").toString().compareTo(o2.get("age").toString());
            }
        }).collect(Collectors.toList());*/
        List<Map> ls4 = lsM.stream().sorted(Comparator.comparing(m -> Integer.valueOf(m.get("age").toString()))).collect(Collectors.toList());
        ls4.forEach(map -> System.out.println(map));
        //结果:
        //{name=bbb, company=B公司, age=19}
        //{name=shaozhiqi, company=A公司, age=20}
        //{name=qqq, company=B公司, age=21}
        //{name=LLL, company=C公司, age=25}
        //{name=LLL, company=C公司, age=25}

        //limit---------------------------------------------------------------------------------------------------------
        List<Map> ls5 = lsM.stream().limit(2).collect(Collectors.toList());
        ls5.forEach(map -> System.out.println("limit:" + map));
        //结果:
        //limit:{name=shaozhiqi, company=A公司, age=20}
        //limit:{name=qqq, company=B公司, age=21}

        //取集合的最小值方法1和方法二都可以,我选择二,1不推荐-------------------------------------------------------------
        //Map map=lsM.stream().min((m1,m2)->m1.get("age").toString().compareTo(m2.get("age").toString())).get();
        Map map = lsM.stream().min(Comparator.comparing(m -> m.get("age").toString())).get();
        System.out.println("min:" + map);
        //结果:
        //min:{name=bbb, company=B公司, age=19}


        //判断是否存在某个人,过滤也行。也有其他的match可以自己测试---------------------------------------------------------
        boolean haveName = lsM.stream().anyMatch(m -> "shaozhiqi".equals(m.get("name").toString()));
        if (haveName) {
            System.out.println("有 shaozhiqi");
        }
        //结果
        //有 shaozhiqi

    

    }
}

 

posted @ 2019-09-26 16:18  ~清风煮酒~  阅读(221)  评论(0编辑  收藏  举报