jdk8 新特性 Lambda表达式

package test;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zyq on 2020/7/6.
 */
@Slf4j
public class BaseLambda {

    public static void testForeach(){
        //定义一个数组
       
String[] array={"AA","BB","CC","DD","EE","FF"};
        //将数组转换成集合
       
List<String> list = Arrays.asList(array);
        //传统的遍历方式
       
log.info("========传统的遍历方式:");
        for(String obj :list){
            log.info("========传统的遍历方式:"+obj);
        }
        // 使用 Lambda 表达式以及函数操作(functional operation)
       
log.info("============使用 Lambda 表达式以及函数操作(functional operation)");
        list.forEach((obj)->log.info(obj));

        log.info("============使用双冒号操作符");
        list.forEach(log::info);
        list.forEach(System.out::println);


    }

    /**
     * Stream
去重复
     * String
Integer 可以使用该方法去重
     */
   
public static void testStreamDuplicates(){

        //定义一个数组
       
String[] array={"AA","BB","CC","DD","EE","FF","AA","BB","CC","DD","EE","FF"};
        // 转换成集合
       
List<String> list = Arrays.asList(array);
        //Stream去重复
       
List<String> collect = list.stream().distinct().collect(Collectors.toList());
        collect.forEach(log::info);


    }

    public static void main(String[] args) {
        testForeach();
        //testStreamDuplicates();
   
}


}

 

运行testForeach()方法:

复制代码
11:35:32.611 [main] INFO test.BaseLambda - ========传统的遍历方式:
11:35:32.615 [main] INFO test.BaseLambda - ========传统的遍历方式:AA
11:35:32.615 [main] INFO test.BaseLambda - ========传统的遍历方式:BB
11:35:32.615 [main] INFO test.BaseLambda - ========传统的遍历方式:DD
11:35:32.615 [main] INFO test.BaseLambda - ========传统的遍历方式:CC
11:35:32.615 [main] INFO test.BaseLambda - ========传统的遍历方式:EE
11:35:32.615 [main] INFO test.BaseLambda - ========传统的遍历方式:FF
11:35:32.615 [main] INFO test.BaseLambda - ============使用 Lambda 表达式以及函数操作(functional operation)
11:35:32.649 [main] INFO test.BaseLambda - AA
11:35:32.650 [main] INFO test.BaseLambda - BB
11:35:32.650 [main] INFO test.BaseLambda - DD
11:35:32.650 [main] INFO test.BaseLambda - CC
11:35:32.650 [main] INFO test.BaseLambda - EE
11:35:32.650 [main] INFO test.BaseLambda - FF
11:35:32.650 [main] INFO test.BaseLambda - ============使用双冒号操作符
11:35:32.651 [main] INFO test.BaseLambda - AA
11:35:32.651 [main] INFO test.BaseLambda - BB
11:35:32.651 [main] INFO test.BaseLambda - DD
11:35:32.651 [main] INFO test.BaseLambda - CC
11:35:32.651 [main] INFO test.BaseLambda - EE
11:35:32.651 [main] INFO test.BaseLambda - FF
AA
BB
DD
CC
EE
FF

Process finished with exit code 0
复制代码

运行testStreamDuplicates()方法:

11:45:42.314 [main] INFO test.BaseLambda - AA
11:45:42.317 [main] INFO test.BaseLambda - BB
11:45:42.318 [main] INFO test.BaseLambda - CC
11:45:42.318 [main] INFO test.BaseLambda - DD
11:45:42.318 [main] INFO test.BaseLambda - EE
11:45:42.318 [main] INFO test.BaseLambda - FF

Process finished with exit code 0

注意:String[] array={"AA","BB","DD","CC","EE","FF"};是不会进行排序的。

 

复制代码
package com.zyq;

import java.util.ArrayList;

public class ForPractise {
    public static void main(String[] args) {
        //创建增强for
        Student stu1 = new Student();
        stu1.setAge(1);
        stu1.setId("1");
        stu1.setName("丫丫丫1");
        Student stu2 = new Student();
        stu2.setAge(2);
        stu2.setId("2");
        stu2.setName("丫丫丫2");
        Student stu3 = new Student();
        stu3.setAge(3);
        stu3.setId("3");
        stu3.setName("丫丫丫3");
        final ArrayList<Student> list = new ArrayList<>();
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        list.forEach(item->{
//            Student student = list.get(item.getAge()-1);
            System.out.println(item);
        });

    }
}
复制代码

 

Student(id=1, name=丫丫丫1, age=1)
Student(id=2, name=丫丫丫2, age=2)
Student(id=3, name=丫丫丫3, age=3)

posted on   ~码铃薯~  阅读(189)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示