AE 2020 6月最新版及全套插件


https://www.52pojie.cn/thread-1216454-1-1.html

package com.example.lambdademo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Lambda表達式
 * 面向過程程序語言:參數傳遞是基本類型的變量
 * 面向對象語言
 * - 傳遞基本類型的變量
 * - 傳遞對象變量
 * 傳遞方法/代碼塊(函數式程序語言設計)
 * - 剛開始,Java為了簡單性,一致性,拒絕此功能
 * - 為了市場和技術的需要,Java8開始,支持此項功能,提出Java的Lambda表達式實現
 *
 * - 類似於匿名方法,一個沒有名字的方法
 * - 參數,箭頭,表達式語句
 * - 可以忽略寫參數類型
 * - 堅決不聲明返回值類型
 * - 沒有public/protected/private/static/final等修飾符
 * - 單句表達式,將直接返回值,不用大括號
 * - 帶return語句,算多句,必須用大括號
 *
 * - 如果有返回值,返回值類型會在上下文推斷出來的,無需聲明
 * - 只在某幾個分支有返回值,這樣是不合法的
 *
 * 函數式接口
 * - 是一個接口,符合java接口的定義
 * - 只包含一個抽象方法的接口
 * - 可以包括其他的default方法,static方法,private方法
 * - 由於只有一個未實現的方法,所以Lambda表達式可以自動填上這個尚未實現的方法
 * - 采用Lambda表達式,可以自動創建出一個(偽)嵌套類的對象(沒有實際的嵌套類class文件產生),
 * 然后使用,比真正嵌套類更加輕量,更加簡潔高效
 *
 * - 只帶有一個未實現的方法,內容簡單
 * - 大量重復性的函數式接口,使得源碼膨脹
 * 系統自帶的函數式接口
 * - 涵蓋大部分常用的功能,可以重復使用
 * - 位於java.util.function包中
 *
 * 系統自帶的函數式接口(部分常用)
 * Predicate<T> 返回值Boolean
 * Consumer<T> 返回值void
 * Function<T,R> 返回值R
 * Supplier<T> 返回值T
 *
 *
 *
 *
 */
@SpringBootApplication
public class LambdaDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(LambdaDemoApplication.class, args);
    }

}

package com.example.lambdademo.consumer;

import java.util.function.Consumer;

/**
 * 函數式接口Consumer,一次性操作,無返回
 */
public class ConsumerDemo {

    public static void main(String[] args) {
        String[] planets = new String[]{
                "Mercury", "Venus", "Earth", "Mars",
                "Jupiter", "Saturn", "Uranus", "Neptune"
        };
        Consumer<String> printer = s ->
                System.out.println("Planet : " + s);

        for (String p : planets) {
            printer.accept(p);
        }
    }
}

package com.example.lambdademo.demo1;

import java.util.Arrays;

public class LambdaDemo1 {

    public static void main(String[] args) {
        String[] planets = new String[]{
                "Mercury","Venus","Earth","Mars",
                "Jupiter","Saturn","Uranus","Neptune"
        };
        System.out.println("使用Lambda,長度從小到大");
        Arrays.sort(planets,(first,second)->(first.length()-second.length()));
        System.out.println(Arrays.toString(planets));
        System.out.println("不使用Lambda,是按照字母順序排序");
        Arrays.sort(planets);
        System.out.println(Arrays.toString(planets));
        System.out.println("使用Lambda,長度從大到小");
        Arrays.sort(planets,(first,second)->(-1)*(first.length()-second.length()));
        System.out.println(Arrays.toString(planets));

    }
}

package com.example.lambdademo.function;

import java.util.function.Function;

/**
 * 函數式接口Function(函數),接收一個參數,返回一個參數
 */
public class FunctionDemo {

    public static void main(String[] args) {
        String[] planets = new String[]{
                "Mercury", "Venus", "Earth", "Mars",
                "Jupiter", "Saturn", "Uranus", "Neptune"
        };
        Function<String, String> upper = s -> s.toUpperCase();
            //可以做更復雜操作
//            return s.toUpperCase();
//        };
        for (String p : planets) {
            System.out.println(upper.apply(p));
        }

        Function<String, Integer> getLen = s -> s.length();
        for (String p : planets) {
            System.out.println(getLen.apply(p));
        }

    }
}

package com.example.lambdademo.predicate;

import java.util.function.Predicate;

/**
 * 函數式接口Predicate 有斷言的意思,返回boolean
 */
public class PredicateDemo {

    public static void main(String[] args) {
        String[] planets = new String[]{
                "Mercury", "Venus", "Earth", "Mars",
                "Jupiter", "Saturn", "Uranus", "Neptune"
        };
        Predicate<String> oddLength = s ->
                s.length() % 2 == 0 ? false : true;

        for (String p : planets) {
            if (oddLength.test(p)) {
                System.out.println("=====奇數=====" + p + "," + p.length());
            } else {
                System.out.println("=====偶數=====" + p + "," + p.length());
            }
        }
    }
}

package com.example.lambdademo.supplier;

import java.util.function.Supplier;

import static java.lang.Math.floor;
import static java.lang.Math.random;

/**
 * 函數式接口Supplier(供應者),無輸入參數,返回一個數據
 */
public class SupplierDemo {

    public static void main(String[] args) {
        String[] planets = new String[]{
                "Mercury", "Venus", "Earth", "Mars",
                "Jupiter", "Saturn", "Uranus", "Neptune"
        };
        Supplier<String> planetFactory = () ->
                planets[(int) floor(random() * 8)];

        for (int i = 0; i < 5; i++) {
            System.out.println(planetFactory.get());
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM