Offer

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"));
posted @ 2020-11-04 11:23  Empirefree  阅读(79)  评论(0编辑  收藏  举报