java8 Lambda

1.计算流中所有数之和

static void test3() {
    List<Integer> 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<HashMap> 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<String> 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<HashMap> list = new ArrayList<>();
        HashMap map1 = new HashMap();
        map1.put("name", "小志1");
        List<HashMap> 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<HashMap> 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<HashMap>) map.get("song")) {
                if (Integer.parseInt(song.get("length").toString()) >= 60) {
                    nameList1.add(song.get("name"));
                }
            }
        }

        //重构代码
        List<String> nameList2 = list.stream()
                .flatMap(f -> ((List<HashMap>) 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<HashMap> 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<HashMap> 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<HashMap> list = Arrays.asList(map1, map2, map3);
        //minBy找最小值
        Optional<HashMap> 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<HashMap> 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.数据分块

 /*数据分块
        将一个集合性别为男的分为一组,其它的为一组,可以使用两次过滤操作,分别过滤,但是这样的操作需要两个流,
        要执行多次,代码也会冗余,这是使用收集器 partitioningBy进行分成两部分
        partitioningBy:根据布尔值返回一 个 Map 到列表,true为指定的一部分,false为其它的一部分
    */
    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<HashMap> list = Arrays.asList(map1, map2, map3, map4);
        Map<Boolean, List<HashMap>> 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.数据分组

 /**数据分组:一种更自然的分组
     * groupingBy:可以指定任意值进行分组,类似数据库的 group by
     */
    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<HashMap> list = Arrays.asList(map1, map2, map3, map4);
        Map<String, List<HashMap>> 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<String> 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 统计分组后的个数)

/**组合收集器
     * 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<HashMap> list = Arrays.asList(map1, map2, map3, map4);
        Map<String, Long> 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<HashMap> list = Arrays.asList(map1, map2, map3, map4);
        Map<String, List<Object>> collect = list.stream().
                collect(Collectors.
                        groupingBy(f -> f.get("sex").toString(), Collectors.mapping(f -> f.get("name"), Collectors.toList())));
        //{
        // 女=[老六],
        // 男=[张三, 李四],
        // 不明=[王五]
        // }
        System.out.println(collect);
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值