栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java8 Lambda

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java8 Lambda

1.计算流中所有数之和
static void test3() {
    List list = new ArrayList<>();
    list.add(12);
    list.add(2);
    int sum = list.stream().mapToInt(f -> f).sum();
    System.out.println(sum);
}
2.filte map 练习
 static void test1() {  
        List list = new ArrayList();
        HashMap map1 = new HashMap();
        map1.put("nationality", "中国");
        map1.put("name", "小志1");
        map1.put("age", 21);
        HashMap map2 = new HashMap();
        map2.put("nationality", "俄罗斯");
        map2.put("name", "小志2");
        map2.put("age", 19);
        HashMap map3 = new HashMap();
        map3.put("nationality", "巴基斯坦");
        map3.put("name", "小志3");
        map3.put("age", 23);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        //1.过滤大于20岁的    filter
        //2.找出所有的国籍    map
        //3.将国籍映射成集合  collect(Collectors.toList())
        List collect = list.stream().
                filter(f -> Integer.parseInt(f.get("age").toString()) > 20).
                map(f -> f.get("nationality").toString()).
                collect(Collectors.toList());

        System.out.println(collect);
    }
3.重构嵌套for循环
static void test2() {
        List list = new ArrayList<>();
        HashMap map1 = new HashMap();
        map1.put("name", "小志1");
        List song1 = new ArrayList();
        HashMap songMap1 = new HashMap();
        songMap1.put("name", "歌曲1");
        songMap1.put("length", "20");
        HashMap songMap2 = new HashMap();
        songMap2.put("name", "歌曲2");
        songMap2.put("length", "70");
        map1.put("song", song1);
        song1.add(songMap1);
        song1.add(songMap2);
        HashMap map2 = new HashMap();
        map2.put("name", "小志2");
        List song2 = new ArrayList();
        HashMap songMap3 = new HashMap();
        songMap3.put("name", "歌曲3");
        songMap3.put("length", "50");
        HashMap songMap4 = new HashMap();
        songMap4.put("name", "歌曲4");
        songMap4.put("length", "80");
        map2.put("song", song2);
        song2.add(songMap3);
        song2.add(songMap4);
        list.add(map1);
        list.add(map2);
        //数据结构[
        // {song=[{name=歌曲1, length=20}, {name=歌曲2, length=70}], name=小志1},
        // {song=[{name=歌曲3, length=50}, {name=歌曲4, length=80}], name=小志2}
        // ]

        //1.找出所有长度length大于60的name
        //旧代码
        List nameList1 = new ArrayList();
        for (HashMap map : list) {
            for (HashMap song : (List) map.get("song")) {
                if (Integer.parseInt(song.get("length").toString()) >= 60) {
                    nameList1.add(song.get("name"));
                }
            }
        }

        //重构代码
        List nameList2 = list.stream()
                .flatMap(f -> ((List) f.get("song")).stream())
                .filter(f -> Integer.parseInt(f.get("length").toString()) >= 60)
                .map(f -> f.get("name").toString())
                .collect(Collectors.toList());
        System.out.println(nameList2);
    }
4.集合中map中有age sex name ,返回map只带age name的集合
 static void test4() {
        List list = new ArrayList();
        HashMap map1 = new HashMap();
        map1.put("age", 12);
        map1.put("sex", "男");
        map1.put("name", "小志1");

        HashMap map2 = new HashMap();
        map2.put("age", 18);
        map2.put("sex", "女");
        map2.put("name", "小志2");
        list.add(map1);
        list.add(map2);
        List collect = list.stream().map(f -> {
            HashMap map = new HashMap();
            map.put("age", f.get("age"));
            map.put("name", f.get("name"));
            return map;
        }).collect(Collectors.toList());

//        List collect=new ArrayList();
//        list.stream().reduce(new HashMap(),(a,b)->{
//            HashMap map = new HashMap();
//            map.put("age", b.get("age"));
//            map.put("name", b.get("name"));
//            collect.add(map);
//            return null;
//        });
        System.out.println(collect);
    }
5. 计算一个字符串中小写字符的个数(filte)
static void test5() {
    String s = "Abcdef";
    long count = s.chars().boxed().filter(f -> Character.isLowerCase(f)).count();
    System.out.println(count);
}
6. 计算一个字符串中小写字符的个数(reduce)
 static void test6() {
        String s = "Abcdef";
        Integer reduce = s.chars().boxed().reduce(0, (a, b) -> {
            if (Character.isLowerCase(b.intValue())) {
                a++;
            }
            return a;
        });
        System.out.println(reduce);
    }
7.找出length最大的map
 static void test7() {
        HashMap map1 = new HashMap();
        map1.put("length", 20);
        HashMap map2 = new HashMap();
        map2.put("length", 40);
        HashMap map3 = new HashMap();
        map3.put("length", 30);
        List list = Arrays.asList(map1, map2, map3);
        //minBy找最小值
        Optional optional = list.stream().collect(Collectors.maxBy(Comparator.comparing(f -> f.get("length").toString())));
        System.out.println(optional.get());
    }
8.计算平均值
static void test8() {
        HashMap map1 = new HashMap();
        map1.put("length", 20);
        HashMap map2 = new HashMap();
        map2.put("length", 40);
        HashMap map3 = new HashMap();
        map3.put("length", 30);
        List list = Arrays.asList(map1, map2, map3);
        Double length = list.stream().collect(Collectors.averagingInt(f -> Integer.parseInt(f.get("length").toString())));
        System.out.println(length);
    }
9.数据分块
 
    static void test9() {
        HashMap map1 = new HashMap();
        map1.put("name", "张三");
        map1.put("sex", "男");
        HashMap map2 = new HashMap();
        map2.put("name", "李四");
        map2.put("sex", "男");
        HashMap map3 = new HashMap();
        map3.put("name", "王五");
        map3.put("sex", "不明");
        HashMap map4 = new HashMap();
        map4.put("name", "老六");
        map4.put("sex", "女");
        List list = Arrays.asList(map1, map2, map3, map4);
        Map> sex = list.stream().collect(Collectors.partitioningBy(f -> "男".equals(f.get("sex"))));
        //{
        // false=[{sex=不明, name=王五}, {sex=女, name=老六}],
        // true=[{sex=男, name=张三}, {sex=男, name=李四}]
        // }
        System.out.println(sex);
    }
10.数据分组
 
    static void test10() {
        HashMap map1 = new HashMap();
        map1.put("name", "张三");
        map1.put("sex", "男");
        HashMap map2 = new HashMap();
        map2.put("name", "李四");
        map2.put("sex", "男");
        HashMap map3 = new HashMap();
        map3.put("name", "王五");
        map3.put("sex", "不明");
        HashMap map4 = new HashMap();
        map4.put("name", "老六");
        map4.put("sex", "女");
        List list = Arrays.asList(map1, map2, map3, map4);
        Map> sex = list.stream().collect(Collectors.groupingBy(f -> f.get("sex").toString()));
        //{
        // 女=[{sex=女, name=老六}],
        // 男=[{sex=男, name=张三}, {sex=男, name=李四}],
        // 不明=[{sex=不明, name=王五}]
        // }
        System.out.println(sex);
    }
11.拼接字符串
 static void test11(){
        List list=Arrays.asList("j","a","v","a","8");
        String s = list.stream().collect(Collectors.joining(",","[","]"));
        //[j,a,v,a,8]
        System.out.println(s);
    }
12.组合收集器(Collectors的groupingBy 和 counting 统计分组后的个数)
    static void test12() {
        HashMap map1 = new HashMap();
        map1.put("name", "张三");
        map1.put("sex", "男");
        HashMap map2 = new HashMap();
        map2.put("name", "李四");
        map2.put("sex", "男");
        HashMap map3 = new HashMap();
        map3.put("name", "王五");
        map3.put("sex", "不明");
        HashMap map4 = new HashMap();
        map4.put("name", "老六");
        map4.put("sex", "女");
        List list = Arrays.asList(map1, map2, map3, map4);
        Map sex = list.stream().collect(Collectors.groupingBy(f -> f.get("sex").toString(), Collectors.counting()));
        System.out.println(sex);
    }
13.组合收集器(Collectors的groupingBy 和 mapping 根据性别分组获取性别对应的名字)
 static void test13() {
        HashMap map1 = new HashMap();
        map1.put("name", "张三");
        map1.put("sex", "男");
        HashMap map2 = new HashMap();
        map2.put("name", "李四");
        map2.put("sex", "男");
        HashMap map3 = new HashMap();
        map3.put("name", "王五");
        map3.put("sex", "不明");
        HashMap map4 = new HashMap();
        map4.put("name", "老六");
        map4.put("sex", "女");
        List list = Arrays.asList(map1, map2, map3, map4);
        Map> collect = list.stream().
                collect(Collectors.
                        groupingBy(f -> f.get("sex").toString(), Collectors.mapping(f -> f.get("name"), Collectors.toList())));
        //{
        // 女=[老六],
        // 男=[张三, 李四],
        // 不明=[王五]
        // }
        System.out.println(collect);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/842869.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号