zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    jquery中Ajax提交配合PHP使用的注意事项-编码
    js根据子目录数目显示父级目录
    smarty中函数的使用以及二维数组的使用
    在浏览器中对访问的网页中的cookie添加和修改
    Windows映射网络驱动器提示错误
    Linux防火墙/iptables使用
    VMWare启动虚拟机失败,提示锁定文件失败解决方法
    虚拟机中Linux设置当前ip
    基于搜索内容自己写一个分页功能
    Newtonsoft.Json
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/13254035.html
Copyright © 2011-2022 走看看