1. 程式人生 > 實用技巧 >Java-再學Lambda

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"));