全网最全Lamdba实用案例(实际开发中绝对用的到)

准备工作:

pom.xml:


    org.springframework.boot
    spring-boot-starter-test
    test


    org.projectlombok
    lombok
    1.18.20

实体:

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Dog {
    private Integer id;
    private String name;
    private String hobby;
    private Integer age;
}

1、Lamdba遍历一个list集合

@Test
public void test(){
    //创建一个list集合
    List list = new ArrayList();
    list.add(new Dog(1,"小白","睡觉",2));
    list.add(new Dog(2,"小黑","洗澡",2));
    list.add(new Dog(3,"小红","吃饭",2));
    list.add(new Dog(4,"小紫","睡觉",2));

    //遍历
    list.stream().forEach((item)->{
        System.out.println(item);
    });
}

控制台输出:

Dog(id=1, name=小白, hobby=睡觉, age=2)
Dog(id=2, name=小黑, hobby=洗澡, age=2)
Dog(id=3, name=小红, hobby=吃饭, age=2)
Dog(id=4, name=小紫, hobby=睡觉, age=2)

2、Lamdba遍历一个map集合

@Test
public void test3(){
    //创建一个map集合
    Map map = new HashMap();
    map.put("张三", "医生");
    map.put("李四", "老师");
    map.put("王五", "护士");
    map.put("赵六", "程序员");
    //遍历
    map.forEach((key,value)->{
        System.out.println(key+":"+value);
    });
}

控制台输出:

李四:老师
张三:医生
王五:护士
赵六:程序员

3、Lamdba遍历一个数组

    @Test
    public void test4(){
        //创建一个数组
        Integer[] arrays = {1, 2, 3, 4};

        //遍历数组(方式一)
//        Arrays.asList(arrays).forEach((item)->{
//            System.out.println(item);
//        });

        //遍历数组(方式二)
        Arrays.stream(arrays).forEach((item)->{
            System.out.println(item);
        });
    }

控制台输出:

1
2
3
4

4、使用Lamdba把一个list集合中的数据添加到另一个list集合

    @Test
    public void test2(){
        //创建一个list集合
        List list = new ArrayList();
        list.add(new Dog(1,"小白","睡觉",2));
        list.add(new Dog(2,"小黑","洗澡",2));
        list.add(new Dog(3,"小红","吃饭",2));
        list.add(new Dog(4,"小紫","睡觉",2));

        //创建一个空的list集合
        List list2 = new ArrayList();

        //输出两个list集合
        System.out.println("list:"+list);
        System.out.println("list2:"+list2);

        System.out.println("------------------处理前后--------------------");

        //使用Lamdba把一个list集合中的数据添加到list2集合(方式一)
//        list2 = list.stream().map((item)->{
//            return item;
//        }).collect(Collectors.toList());

        //使用Lamdba把一个list集合中的数据添加到list2集合(方式二)
        list.stream().forEach((item)->{
            list2.add(item);
        });

        //输出list2集合
        System.out.println("list2:"+list2);
    }

控制台输出:

list:[Dog(id=1, name=小白, hobby=睡觉, age=2), Dog(id=2, name=小黑, hobby=洗澡, age=2), Dog(id=3, name=小红, hobby=吃饭, age=2), Dog(id=4, name=小紫, hobby=睡觉, age=2)]
list2:[]
------------------处理前后--------------------
list2:[Dog(id=1, name=小白, hobby=睡觉, age=2), Dog(id=2, name=小黑, hobby=洗澡, age=2), Dog(id=3, name=小红, hobby=吃饭, age=2), Dog(id=4, name=小紫, hobby=睡觉, age=2)]

5、使用Lamdba将数组中的数据遍历到list集合中去

@Test
public void test5(){
    //创建一个数组
    Integer[] arrays = {1, 2, 3, 4};
    //创建一个list集合
    List list = new ArrayList();
    //遍历arrays
    System.out.println("arrays:");
    Arrays.asList(arrays).forEach((item)->{
        System.out.println(item);
    });
    //遍历list
    System.out.println("list:");
    list.stream().forEach((item)->{
        System.out.println(item);
    });
    System.out.println("------------------处理前后--------------------");
    //将数组中的数据遍历到list集合中去
    Arrays.asList(arrays).forEach((item)->{
        list.add(item);
    });
    //遍历list
    System.out.println("list:"+list);
}

控制台输出:

arrays:
1
2
3
4
list:
------------------处理前后--------------------
list:[1, 2, 3, 4]

6、使用Lamdba将list中的数据遍历到map集合中去

@Test
public void test6(){
    //创建一个list集合
    List list = new ArrayList();
    list.add(new Dog(1,"小白","睡觉",2));
    list.add(new Dog(2,"小黑","洗澡",2));
    list.add(new Dog(3,"小红","吃饭",2));
    list.add(new Dog(4,"小紫","睡觉",2));
    //创建一个map集合
    Map map = new HashMap();
    //遍历map
    System.out.println("map:"+map);
    System.out.println("------------------处理前后--------------------");
    //将list中的数据遍历到map集合中去
    list.stream().forEach((item)->{
        map.put(item.getId(),item.getName());
    });
    //遍历map
    System.out.println("map:"+map);
    System.out.println("map的类型:"+map.getClass().getSimpleName());
}

控制台输出:

map:{}
------------------处理前后--------------------
map:{1=小白, 2=小黑, 3=小红, 4=小紫}
map的类型:HashMap

7、使用Lamdba表达式拆分一个字符串

@Test
public void test8(){
    //定义一个str字符串
    String str = "小白,小绿,小黑,小红";
    //定义一个list集合
    List list = new ArrayList();
    //将str字符串以逗号拆分
    Arrays.stream(str.split(",")).forEach((item)->{
        list.add(item);
    });
    //输出list
    System.out.println("list:"+list);
    System.out.println("arrays:"+list.getClass().getSimpleName());
}

控制台输出:

list:[小白, 小绿, 小黑, 小红]
arrays:ArrayList

8、使用Lamdba表达式过滤集合中包含“红红”的元素

@Test
public void test9(){
    //创建一个list集合
    List list = new ArrayList<>();
    list.add(new Dog(1,"小白","睡觉",2));
    list.add(new Dog(2,"小红红","洗澡",2));
    list.add(new Dog(3,"大红","吃饭",2));
    list.add(new Dog(4,"小紫","睡觉",2));
    System.out.println("list:"+list);
    //再创建一个list集合
    List list2 = new ArrayList<>();
    System.out.println("list2:"+list2);
    System.out.println("------------------处理前后--------------------");
    //过滤集合中包含“红红”的元素
    list.stream().filter(item -> item.getName().indexOf("红红")!=-1)
            .forEach(item ->list2.add(item));
    System.out.println("list2:"+list2);
}

控制台输出:

list:[Dog(id=1, name=小白, hobby=睡觉, age=2), Dog(id=2, name=小红红, hobby=洗澡, age=2), Dog(id=3, name=大红, hobby=吃饭, age=2), Dog(id=4, name=小紫, hobby=睡觉, age=2)]
list2:[]
------------------处理前后--------------------
list2:[Dog(id=2, name=小红红, hobby=洗澡, age=2)]

9、使用Lamdba表达式取出list集合中的所有id

@Test
public void test10(){
    //创建一个list集合
    List list = new ArrayList<>();
    list.add(new Dog(1,"小白","睡觉",2));
    list.add(new Dog(2,"小红","洗澡",2));
    list.add(new Dog(3,"大红","吃饭",2));
    list.add(new Dog(4,"小紫","睡觉",2));
    //取出list集合中的所有id
    List list2 = list.stream().map(Dog::getId).collect(Collectors.toList());
    System.out.println(list2);
    System.out.println(list2.getClass().getSimpleName());
}

控制台输出:

[1, 2, 3, 4]
ArrayList

10、使用Lamdba表达式取出list集合中的key和value

@Test
public void test11(){
    //创建一个list集合
    List list = new ArrayList<>();
    list.add(new Dog(1,"小白","睡觉",2));
    list.add(new Dog(2,"小红","洗澡",2));
    list.add(new Dog(3,"大红","吃饭",2));
    list.add(new Dog(4,"小紫","睡觉",2));
    //取出list集合中的key和value
    //需要保证生成的map中的key的唯一性,否则会报 java.lang.IllegalStateException: Duplicate key 错误
    Map map = list.stream()
            .collect(Collectors.toMap(Dog::getId, Dog::getName));
    System.out.println("map为:"+map);
    //再创建一个list集合
    List list2 = new ArrayList<>();
    list2.add(new Dog(1,"小白","睡觉",2));
    list2.add(new Dog(2,"小红","洗澡",2));
    list2.add(new Dog(2,"大红","吃饭",2));
    list2.add(new Dog(4,"小紫","睡觉",2));
    //取出list集合中的key和value(方法二)
    //如果出现重复key,则会用原来的key代替新的key
    Map map2 = list2.stream().
            collect(Collectors.toMap(Dog::getId, Dog::getName, (o, n) -> o));
    System.out.println("map2为:"+map2);
    //java.lang.IllegalStateException: Duplicate key 错误测试
    Map map3 = list2.stream()
            .collect(Collectors.toMap(Dog::getId, Dog::getName));
    System.out.println("map3为:"+map3);
}

控制台输出:

map为:{1=小白, 2=小红, 3=大红, 4=小紫}
map2为:{1=小白, 2=小红, 4=小紫}


java.lang.IllegalStateException: Duplicate key 小红

11、使用Lamdba表达式实现分页

    @Test
    public void test12(){
        //创建一个list集合
        List list = new ArrayList<>();
        list.add(new Dog(1,"小白","睡觉",2));
        list.add(new Dog(2,"小红","洗澡",2));
        list.add(new Dog(3,"小黄","吃饭",2));
        list.add(new Dog(4,"小紫","睡觉",2));
        list.add(new Dog(5,"小蓝","洗澡",2));
        list.add(new Dog(6,"小绿","睡觉",2));
        list.add(new Dog(7,"小黑","吃饭",2));

//        List list2 = list.stream().skip(pageSize * (pageNo - 1)).limit(pageSize)
//                .collect(Collectors.toList());
        //使用Lamdba表达式实现分页
        List list2 = list.stream().skip(0).limit(3)
                .collect(Collectors.toList());
        System.out.println("list2:"+list2);
    }

控制台输出:

list2:[Dog(id=1, name=小白, hobby=睡觉, age=2), Dog(id=2, name=小红, hobby=洗澡, age=2), Dog(id=3, name=小黄, hobby=吃饭, age=2)]

12、使用Lamdba表达式实现分页,并且带条件

@Test
public void test13(){
    //创建一个list集合
    List list = new ArrayList<>();
    list.add(new Dog(1,"小白","睡觉",2));
    list.add(new Dog(2,"小红","洗澡",2));
    list.add(new Dog(3,"小黄","吃饭",2));
    list.add(new Dog(4,"小紫","睡觉",2));
    list.add(new Dog(5,"小蓝","洗澡",2));
    list.add(new Dog(6,"小绿","睡觉",2));
    list.add(new Dog(7,"小黑","吃饭",2));
    //使用Lamdba表达式实现分页
    List list2 = list.stream().filter(item->item.getHobby().equals("睡觉"))
            .skip(0).limit(2).collect(Collectors.toList());
    System.out.println("list2:"+list2);
}

控制台输出:

list2:[Dog(id=1, name=小白, hobby=睡觉, age=2), Dog(id=4, name=小紫, hobby=睡觉, age=2)]

其他案例后续还会持续更新,期待你的关注哦~

你可能感兴趣的:(java,Lambda)