Java中Lambda表达式的简单使用

  Lambda表达式是Java SE 8中一个重要的新特性。你可以把 Lambda表达式 理解为是一段可以传递的代码 (将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使Java的语言表达能力得到了提升。

  Lambda运算符:所有的Lambda表达式都是用新的Lambda运算符 " => ",可以叫他,“转到”或者 “成为”。运算符将表达式分为两部分,左边指定输入参数,右边是Lambda的主体。

  在平常 我们可以要通过sql 对数据进行过滤、分组、转化等,如果非常频繁,会对数据库增加负担,我们这里就可以简单使用Lambda表达式

1、准备一个Object类

import java.math.BigDecimal;

public class AObject {

    private String a;
    
    private String b;
    
    private BigDecimal money;
    
    private Integer num;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }
    
    public BigDecimal getMoney() {
        return money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    @Override
    public String toString(){
        return a+" "+b;
    }
    
    
}

2、Lambda使用

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamUtils {
    
    public static void main(String args[]){
        listToMap();
        filter();
//        groupingBy();
//        sum();
        
    }

    /**
     * List<T> -> Map
     * 需要注意的是:
     * toMap 如果集合对象有重复的key,会报错Duplicate key ....
     *  可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
     */
    public static void listToMap() {
        List<AObject> aList = new ArrayList<>();
        AObject aObject = new AObject();
        aObject.setA("a");
        aObject.setB("B");
        AObject aObject1 = new AObject();
        aObject1.setA("a1");
        aObject1.setB("B1");
        aList.add(aObject);
        aList.add(aObject1);
        
        Map<String, AObject> map = aList.stream().
                collect(Collectors.toMap(AObject::getA, AObject->AObject,(k1,k2)->k1));
        for(Map.Entry<String, AObject> entry : map.entrySet()){
            System.out.println("key:"+entry.getKey()+"  value:"+entry.getValue().toString());
        }
//        key:a1  value:a1 B1
//        key:a  value:a B
        
        Map<String, String> map1 = aList.stream().
                collect(Collectors.toMap(AObject::getA, AObject::getB,(k1,k2)->k1));
        for(Map.Entry<String, String> entry : map1.entrySet()){
            System.out.println("key:"+entry.getKey()+"  value:"+entry.getValue());
        }
//        key:a1  value:B1
//        key:a  value:B
        
    }
    
    /**
     * 过滤
     */
    public static void filter() {
        List<String> aList = new ArrayList<>();
        aList.add("1");
        aList.add("a");
        List<String> slist =aList.stream().filter(e -> "a".equals(e)).collect(Collectors.toList());
        System.out.println(slist.toString());//        [a]
                
        
        List<AObject> list = new ArrayList<>();
        AObject aObject = new AObject();
        aObject.setA("110000");
        aObject.setB("北京");
        AObject aObject2 = new AObject();
        aObject2.setA("330000");
        aObject2.setB("浙江");
        list.add(aObject);
        list.add(aObject2);
        List<AObject> newList =list.stream().filter(e -> e.getA().equals("110000")).collect(Collectors.toList());
        System.out.println(newList.toString());//        [110000 北京]
    }
    
    /**
     * 分组
     */
    public static void groupingBy(){
        List<AObject> list = new ArrayList<>();
        AObject aObject = new AObject();
        aObject.setA("110000");
        aObject.setB("北京");
        AObject aObject1 = new AObject();
        aObject1.setA("110000");
        aObject1.setB("北京1");
        AObject aObject2 = new AObject();
        aObject2.setA("330000");
        aObject2.setB("浙江");
        AObject aObject3 = new AObject();
        aObject3.setA("330000");
        aObject3.setB("浙江1");
        list.add(aObject);
        list.add(aObject1);
        list.add(aObject2);
        list.add(aObject3);
        //groupingBy  只传key 默认把整个集合关联,  也可以把集合某个数据属性 当做集合取出来,如下groupBy1
        Map<String, List<AObject>> groupBy = list.stream().collect(Collectors.groupingBy(AObject::getA));
        for(Map.Entry<String, List<AObject>> item : groupBy.entrySet()){
            System.out.print(item.getKey()+" : ");
            for(AObject a : item.getValue()){
                System.out.print(a.toString()+";");
            }
            System.out.println();
        }
        //110000 : 110000 北京;110000 北京1;
        //330000 : 330000 浙江;330000 浙江1;
        
        Map<String, List<String>> groupBy1 = list.stream().collect(
                Collectors.groupingBy(AObject::getA, Collectors.mapping(AObject::getB, Collectors.toList())));
        for(Map.Entry<String, List<String>> item : groupBy1.entrySet()){
            System.out.print(item.getKey()+" : ");
            for(String a : item.getValue()){
                System.out.print(a+";");
            }
            System.out.println();
        }
//        110000 : 北京;北京1;
//        330000 : 浙江;浙江1;
    }
    
    /**
     * 求和
     */
    public static void sum(){
        List<AObject> list = new ArrayList<>();
        AObject aObject = new AObject();
        aObject.setMoney(new BigDecimal("100"));
        aObject.setNum(100);
        AObject aObject1 = new AObject();
        aObject1.setMoney(new BigDecimal("200"));
        aObject1.setNum(200);
        AObject aObject2 = new AObject();
        aObject2.setMoney(new BigDecimal("300"));
        aObject2.setNum(300);
        AObject aObject3 = new AObject();
        aObject3.setMoney(new BigDecimal("400"));
        aObject3.setNum(400);
        list.add(aObject);
        list.add(aObject1);
        list.add(aObject2);
        list.add(aObject3);
        //注意的是  求和的属性 值不能为null
        BigDecimal totalMoney = list.stream().map(AObject::getMoney).
                reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println("totalMoney:"+totalMoney); // totalMoney:1000
        Integer totalMoney1 = list.stream().map(AObject::getNum).
                reduce(0, Integer::sum);
        System.out.println("totalSum:"+totalMoney1); // totalSum:1000
        int sum = list.stream().mapToInt(AObject::getNum).sum();
        System.out.println("sum:"+sum);// sum:1000
    }
    
    
}