Lamda表达式

Lamda表达式

一、基本使用

1、没有参数,没有返回值

image-20201113112146865

2、有一个参数,没有返回值

image-20201113112242047

3、有多个参数.没有返回值

image-20201113112325134

4、没有参数,有返回值

image-20201113112429598

5、有一个参数,有返回值

image-20201113112527971

6、有多个参数,有返回值

image-20201113112649310

二、集合操作

1、遍历集合
@Test
    public void myTest1(){
        List<String> strings = new ArrayList<>();
        strings.add("a");
        strings.add("b");
        strings.add("c");
        strings.add("d");
        /*利用stream().foreach()遍历集合*/
        strings.stream().forEach(item->{
            System.out.println(item+" ");
        });
    }
2、改数据并赋值给另一个集合
@Test
    public void myTest2(){
        List<String> strings = new ArrayList<>();
        strings.add("a");
        strings.add("b");
        strings.add("c");
        strings.add("d");

        List<String> list=new ArrayList<>();

        list=strings.stream().map(item->item.toUpperCase()).collect(Collectors.toList());
        list.stream().forEach(item->{
            System.out.println(item);
        });
    }
3、对集合的过滤处理
@Test
    public void myTest3(){
        List<String> strings = new ArrayList<>();
        strings.add("a");
        strings.add("b");
        strings.add("c");
        strings.add("d");

        List<String> list =new ArrayList<>();
        list=strings.stream().filter(item->item.equals("c")).collect(Collectors.toList());
        list.stream().forEach(item->{  //这里遍历可以进行其他操作
            if(item.equals("c")){
                System.out.println("我拦截了c");
            }else{
                System.out.println("我没有拦截"+item);
            }
        });
    }
4、求集合中的max,min,count,avg
@Test
    public void myTest4(){
        List<Integer> strings = new ArrayList<>();
        strings.add(1);
        strings.add(2);
        strings.add(3);
        strings.add(4);

        IntSummaryStatistics stat=strings.stream().mapToInt((item)->item).summaryStatistics();
        System.out.println("max:"+stat.getMax());
        System.out.println("min:"+stat.getMin());
        System.out.println("sum:"+stat.getSum());
        System.out.println("count:"+stat.getCount());
        System.out.println("average:"+stat.getAverage());
    }
5、List转化为Map(方式一)
@Test
    public void myTest5(){
        List<Account> strings = new ArrayList<>();
        strings.add(new Account(1,"a"));
        strings.add(new Account(2,"b"));
        strings.add(new Account(3,"c"));
        strings.add(new Account(4,"d"));
        // Account::getId 作为map 的key  可以自定义
        // account -> account 是用Account实体类作为value
        // account -> {return account.getName();} 是使用 account.getName() 作为map中的value
        Map<Integer, String> map = strings.stream().collect(Collectors.toMap(Account::getId, account -> {
            return account.getName();
        }));
        for (Integer integer : map.keySet()) {
            System.out.println(map.get(integer));
        }
    }
6、List转化Map(方式二)
@Test
    public void myTest6(){
        List<Account> strings = new ArrayList<>();
        strings.add(new Account(1,"a"));
        strings.add(new Account(2,"b"));
        strings.add(new Account(3,"c"));
        strings.add(new Account(4,"d"));
        // Account::getId 作为map 的key  可以自定义
        // Function.identity() 返回总是返回其输入参数的函数
        Map<Integer, Account> map = strings.stream().collect(Collectors.toMap(Account::getId, Function.identity()));
        for (Integer integer : map.keySet()) {
            System.out.println(map.get(integer));
        }
    }
7、将List转成Map(按某个字段分组)
@Test
    public void myTest7(){
        List<Account> strings = new ArrayList<>();
        strings.add(new Account(1,"a"));
        strings.add(new Account(2,"b"));
        strings.add(new Account(3,"c"));
        strings.add(new Account(4, "a"));
        // Account::getId 作为map 的key  可以自定义
        Map<Integer, List<Account>> map = strings.stream().collect(Collectors.groupingBy(Account::getId));

        for (Integer integer : map.keySet()) {
            System.out.println(map.get(integer));
        }

        /*
        *   [Account(id=1, name=a), Account(id=1, name=d)]
            [Account(id=2, name=b)]
            [Account(id=3, name=c)]
        * */
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值