zoukankan      html  css  js  c++  java
  • Java-再学Lambda

    Lambda目录

    1. 4个函数式接口的使用
    2. Lambda与optional
    3. 高级集合类与收集器
    4. 小拓展:JSONObject的使用
    

    函数式接口不必多说,在lambda中有简写。lambda基础与optional判空可以简化代码。高级集合类有分类list,整合list,拼接list功能。

    MarkerHub:参考链接

    package lambda.lambda;
    
    
    import java.math.BigDecimal;
    import java.util.*;
    import java.util.function.Consumer;
    import java.util.function.Function;
    import java.util.function.Predicate;
    import java.util.function.Supplier;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    /**
     * @ProjectName: test
     * @Package: lambda.lambda
     * @Description:
     * @Author: huyuqiao
     * @CreateDate: 2020/11/3 16:15
     */
    public class test {
        public static void main(String[] args){
            //4个函数式接口
            Predicate<Integer> predicate = x -> x > 185;
            Student student = new Student("saic_com", 21, 111);
            System.out.println("isHigher 185" + predicate.test(student.getHeight()));
    
            Consumer<String> consumer = System.out::println;
            consumer.accept("testConsumber");
    
            Function<Student, String> function = Student::getName;
            System.out.println(function.apply(student));
    
            Supplier<Integer> supplier = () -> Integer.valueOf(BigDecimal.TEN.toString());
            System.out.println(supplier.get());
    
            //Lambda---表达式
            List<Student> students = Stream.of(new Student("1", 11, 111),
                    new Student("2", 2, 222),
                    new Student("3", 33, 333)
                    ).collect(Collectors.toList());
            System.out.println(students.toString());
    
            List<Student> students2 = new ArrayList<>();
            students2.add(new Student("1", 11, 111));
            students2.add(new Student("2", 2, 222));
            students2.add(new Student("3", 11, 333));
            students2.stream().map(s -> s.getName()).filter(s -> s.equals("1")).forEach(System.out::println);
    
            // 合并2个list
            Stream.of(students2, Arrays.asList(new Student("3", 33, 333), new Student("3", 33, 333)))
                    .flatMap(s -> s.stream()).forEach(System.out::println);
    
            Optional<Integer> max = students2.stream().map(s -> s.getAge()).max(((o1, o2) -> o1-o2));
            max.ifPresent(System.out::println);
            System.out.println(max.orElse(5));
            System.out.println(max.get());
            // List中数组累加
            System.out.println(students2.stream().map(s -> s.getAge()).reduce(0, (acc, x) -> acc + x));
            students2.remove(1);
            // 移除List中的某个数据
            System.out.println(students2);
    
    
    
            // 字符串拼接
            System.out.println(students2.stream().map(Student::getName).collect(Collectors.joining(",", "[", "]")));
    
            //分组---年龄与list
            Map<Integer, List<Student>> listMap =  students2.stream().collect(Collectors.groupingBy(s -> s.getAge()));
            for (Map.Entry<Integer, List<Student>> listEntry : listMap.entrySet()){
                System.out.println(listEntry.getKey() + " " + listEntry.getValue());
            }
            //分块---Boolean与list
            Map<Boolean, List<Student>> listMap1 = students2.stream().collect(Collectors.partitioningBy(s -> s.getAge() == 11));
            for (Map.Entry<Boolean, List<Student>> entry : listMap1.entrySet()){
                System.out.println(entry.getKey() + " " + entry.getValue());
            }
    
            List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
            list.parallelStream().forEach(System.out::println);
        }
    
    }
    
    

    JSONObject使用:

    将list转换String类型json,然后再获取json中的数据

            String json = JSON.toJSONString(userService2.select().get(0));
            System.out.println(json);
            JSONObject jsonObject = JSON.parseObject(json);
            System.out.println(jsonObject.getLong("id"));
    
  • 相关阅读:
    谷歌浏览器内核自带长截图
    js文件导入swiper方法及分页器不显示原因
    Error: Cannot find module 'gifsicle'问题解决
    新型横向移动工具原理分析、代码分析、优缺点以及检测方案
    武汉解封一周年
    JAVA线程池ThreadPoolExecutor的分析和使用(新手踩坑和推荐方案)
    JAVA常量池
    Java String的intern()注意事项(分JDK1.6及JDK1.7)
    JAVA的类加载过程
    react hooks方法获取不到最新的state解决方法
  • 原文地址:https://www.cnblogs.com/meditation5201314/p/13924966.html
Copyright © 2011-2022 走看看