1. 程式人生 > 程式設計 >淺談Java中FastJson的使用

淺談Java中FastJson的使用

目錄

1、函式式介面概述

函式式介面: 有且僅有一個抽象方法的介面

Java 中的函數語言程式設計體現就是 Lambda 表示式,所以函式式介面就是可以適用於 Lambda 使用的介面,只有確保介面中有且僅有一個抽象方法,Java 中的 Lambda 才能順利地進行推導。

如何檢測一個介面是不是函式式介面:@FunctionalInterface 註解放在介面定義的上方,如果介面是函式式介面編譯通過,如果不是編譯失敗。

注意: 自定義函式式介面時,@FunctionalInterface 是可選的,只要保證滿足函式式介面定義的條件,也照樣是函式式介面。但是建議加上該註解(規範)。

示例

@FunctionalInterface
public interface MyFunctionalInterface {
    void show();
}

2、函式式介面作為方法的引數

示例

public class Test_01 {
    public static void main(String[] args) {
        // 匿名內部類方式
        startRunable(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " 執行緒啟動");
            }
        });

        // lambda方式
        startRunable(() -> System.out.println(Thread.currentThread().getName() + " 執行緒啟動"));
    }
	
    // Runnable類是一個函式s介面
    public static void startRunable(Runnable r) {
        new Thread(r).start();
    }
}
Thread-0 執行緒啟動
Thread-1 執行緒啟動

3、函式式介面作為方法的返回值

示例: 按照字串長度正序排序

public class Test_02 {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("bbb");
        list1.add("a");
        list1.add("cc");
        System.out.println("排序前 = " + list1);
        Collections.sort(list1);
        System.out.println("預設排序後 = " + list1);

        ArrayList<String> list2 = new ArrayList<>();
        list2.add("bbb");
        list2.add("a");
        list2.add("cc");
        System.out.println("排序前 = " + list2);
        Collections.sort(list2, getComparator());
        System.out.println("自定義排序後 = " + list2);
    }

    // 按照字串長度正序排序,將Comparator函式式介面作為返回值

    // lambda方式
    public static Comparator<String> getComparator() {
        return (s1, s2) -> s1.length() - s2.length();
    }

    // 方法引用方式
    public static Comparator<String> getComparator2() {
        return Comparator.comparingInt(String::length);
    }

    // 匿名內部類方式
    public static Comparator<String> getComparator3() {
        return new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.length() - s2.length();
            }
        };
    }
}
排序前 = [bbb, a, cc]
預設排序後 = [a, bbb, cc]
排序前 = [bbb, a, cc]
自定義排序後 = [a, cc, bbb]

4、常用函式式介面

Java 8 在 java.util.function 包下預定義了大量的函式式介面供我們使用

4.1、Supplier 介面

Supplier 介面被稱為生產型介面,如果指定了介面的泛型是什麼型別,那麼介面中的 get 方法就會生產什麼型別的資料供我們使用,T - 出參型別,沒有入參

方法 說明
T get() 該方法不需要引數,它會按照某種實現邏輯(由 Lambda 表示式實現)返回一個數據

示例

public class Test_03 {
    public static void main(String[] args) {
        String name = getString(() -> "勳悟空");
        System.out.println("name = " + name);

        Integer age = getInteger(() -> 500);
        System.out.println("age = " + age);
    }

    // 返回 String 型別資料
    public static String getString(Supplier<String> sup) {
        return sup.get();
    }

    // 返回 Integer 型別資料
    public static Integer getInteger(Supplier<Integer> sup) {
        return sup.get();
    }

    // 返回其它型別資料...
}
name = 勳悟空
age = 500

練習: 獲取 int 陣列最大值

public class Test_04 {
    public static void main(String[] args) {
        int arr[] = {5, 10, 7, 9, 8};
        int maxValue = getMax(() -> {
            int max = arr[0];
            for (int i = 1; i < arr.length; i++) {
                if (max < arr[i]) {
                    max = arr[i];
                }
            }
            return max;
        });
        System.out.println("maxValue = " + maxValue);
    }

    public static int getMax(Supplier<Integer> sup) {
        return sup.get();
    }
}
10

4.2、Consumer 介面

Consumer 介面被稱為消費型介面,它消費的資料的資料型別由泛型指定,T - 入參型別,沒有出參

方法 說明
void accept(T t) 對給定的引數執行此操作
default Consumer andThen(Consumer after) 依次執行此操作,然後執行 after 操作

示例

public class Test_05 {
    public static void main(String[] args) {
        // 列印該字串,消費了一次
        operatorString("勳悟空", System.out::println);
        
        // 先列印該字串,再獲取該字串的長度,消費了兩次
        operatorString("勳悟空", System.out::println, s -> System.out.println(s.length()));
    }

    // 方法1:消費一個字串資料
    public static void operatorString(String name, Consumer<String> con) {
        con.accept(name);
    }

    // 方法2:對一個字串資料連續消費兩次
    public static void operatorString(String name, Consumer<String> con1, Consumer<String> con2) {
        // con1.accept(name);
        // con2.accept(name);

        // 寫法優化
        con1.andThen(con2).accept(name);
    }
}
勳悟空
勳悟空
3

練習

  • 題目

    • String[] strArray = {"林青霞,30", "張曼玉,35", "王祖賢,33"};

    • 字串陣列中有多條資訊,請按照格式:“姓名:XX,年齡:XX"的格式將資訊打印出來

  • 要求

    1. 把列印姓名的動作作為第一個 Consumer 介面的 Lambda 例項

    2. 把列印年齡的動作作為第二個 Consumer 介面的 Lambda 例項

    3. 將兩個 Consumer 介面按照順序組合到一起使用

public class Test_06 {
    public static void main(String[] args) {
        String[] strArray = {"林青霞,30", "張曼玉,35", "王祖賢,33"};
        printInfo(strArray,
                  str -> System.out.print("姓名:" + str.split(",")[0]),
                  str -> System.out.println(",年齡:" + str.split(",")[1]));
    }

    public static void printInfo(String[] strArray, Consumer<String> con1, Consumer<String> con2) {
        for (String s : strArray) {
            con1.andThen(con2).accept(s);
        }
    }
}
姓名:林青霞,年齡:30
姓名:張曼玉,年齡:35
姓名:王祖賢,年齡:33

4.3、Predicate 介面

Predicate 介面通常用於判斷引數是否滿足指定的條件,T - 入參型別,出參為 boolean 型別

方法 說明
boolean test(T t) 對給定的引數進行判斷(判斷邏輯由 Lambda 表示式實現),返回一個布林值
default Predicate negate() 返回一個邏輯的否定,對應 邏輯非( ! )
default Predicate and(Predicate other) 返回一個組合判斷,對應 短路與(&&)
default Predicate or(Predicate other) 返回一個組合判斷,對應 短路或(||)

示例

public class Test_07 {
    public static void main(String[] args) {
        // 方法1
        boolean b1 = checkString1("hello", s -> s.length() > 6); // false
        boolean b2 = checkString1("hello", s -> s.length() < 6); // true
        System.out.println("b1 = " + b1);
        System.out.println("b2 = " + b2);
        System.out.println("--------");

        // 方法2
        boolean b3 = checkString2("hello", s -> s.length() > 6); // true
        boolean b4 = checkString2("hello", s -> s.length() < 6); // false
        System.out.println("b3 = " + b3);
        System.out.println("b4 = " + b4);
        System.out.println("--------");

        // 方法3
        boolean b5 = checkString3("hello", s -> s.length() > 6, s -> s.length() < 6); // false
        boolean b6 = checkString3("hello", s -> s.length() > 3, s -> s.length() < 6); // true
        System.out.println("b5 = " + b5);
        System.out.println("b6 = " + b6);
        System.out.println("--------");

        // 方法4
        boolean b7 = checkString4("hello", s -> s.length() > 6, s -> s.length() < 6); // true
        boolean b8 = checkString4("hello", s -> s.length() > 3, s -> s.length() < 6); // true
        System.out.println("b7 = " + b7);
        System.out.println("b8 = " + b8);
    }

    // 方法1:判斷給定字串是否滿足要求
    public static boolean checkString1(String s, Predicate<String> pre) {
        return pre.test(s);
    }

    // 方法2:判斷給定字串是否滿足要求(結果取反)
    public static boolean checkString2(String s, Predicate<String> pre) {
        return pre.negate().test(s);
    }

    // 方法3:對同一個字串給出兩個不同的判斷條件,再把這兩個結果做 短路與 運算,得出的結果做最終結果
    public static boolean checkString3(String s, Predicate<String> pre1, Predicate<String> pre2) {
        return pre1.and(pre2).test(s);
    }

    // 方法4:對同一個字串給出兩個不同的判斷條件,再把這兩個結果做 短路或 運算,得出的結果做最終結果
    public static boolean checkString4(String s, Predicate<String> pre1, Predicate<String> pre2) {
        return pre1.or(pre2).test(s);
    }
}
b1 = false
b2 = true
--------
b3 = true
b4 = false
--------
b5 = false
b6 = true
--------
b7 = true
b8 = true

練習

  • 題目

    • String[] strArray = {"林青霞,30", "西施,34", "張曼玉,35", "貂蟬,31", "王祖賢,33"};

    • 字串陣列中有多條資訊,請通過 Predicate 介面的拼裝將符合要求的字串篩選到集合 ArrayList 中,並遍歷 ArrayList 集合

    • 同時滿足如下要求:姓名長度大於2,年齡大於33

  • 分析

    • 有兩個判斷條件,所以需要使用兩個 Predicate 介面對條件進行判斷
    • 必須同時滿足兩個條件,所以可以使用 and 方法連線兩個判斷條件
public class Test_08 {
    public static void main(String[] args) {
        String[] strArray = {"林青霞,30", "柳巖,34", "張曼玉,35", "貂蟬,31", "王祖賢,33"};
        ArrayList<String> arrayList = filterData(strArray,
                                                 s -> s.split(",")[0].length() > 2,
                                                 s -> Integer.parseInt(s.split(",")[1]) > 33);
        for (String s : arrayList) {
            System.out.println("s = " + s);
        }
    }

    public static ArrayList<String> filterData(String[] strArray, Predicate<String> pre1, Predicate<String> pre2) {
        ArrayList<String> arrayList = new ArrayList<>();
        for (String s : strArray) {
            if (pre1.and(pre2).test(s)) {
                arrayList.add(s);
            }
        }
        return arrayList;
    }
}
s = 張曼玉,35

4.4、Function 介面

Function<T,R> 介面通常用於對引數進行處理,轉換(處理邏輯由 Lambda 表示式實現),然後返回一個新的值,T - 入參型別,R - 出參型別

方法 說明
R apply(T t) 將此函式應用於給定的引數
default Function andThen(Function after) 返回一個組合函式,首先將該函式應用於輸入,然後將 after 函式應用於結果

示例

public class Test_09 {
    public static void main(String[] args) {
        // 方法1
        convert("1", Integer::parseInt);
        
        // 方法2
        convert(1, i -> String.valueOf(i + 10));
        
        // 方法3
        convert("1", Integer::valueOf, s -> String.valueOf(s + 100));
    }

    // 方法1:將一個字串轉換為Integer型別並在控制檯輸出
    public static void convert(String s, Function<String, Integer> fun) {
        Integer i = fun.apply(s);
        System.out.println("i = " + i);
    }

    // 方法2:將一個Integer型別資料加上一個整數之後,轉換為字串並在控制檯輸出
    public static void convert(int i, Function<Integer, String> fun) {
        String s = fun.apply(i);
        System.out.println("s = " + s);
    }

    // 方法3:將一個字串轉換為Integer型別,把Integer型別資料加上一個整數之後,再轉為字串並在控制檯輸出
    public static void convert(String s, Function<String, Integer> fun1, Function<Integer, String> fun2) {
        String ss = fun1.andThen(fun2).apply(s);
        System.out.println("ss = " + ss);
    }
}
i = 1
s = 11
ss = 101

練習

  • 題目

    • String s = "勳悟空,500";
  • 要求

    1. 將字串擷取得到數字年齡部分

    2. 將上一步的年齡字串轉換成為 Integer 型別資料

    3. 將上一步的 Integer 資料加 23,得到一個 Integer 結果,在控制檯輸出

    4. 請通過 Function 介面來實現函式拼接

public class Test_10 {
    public static void main(String[] args) {
        String monkey = "勳悟空,500";
        convert(monkey,
                s -> monkey.split(",")[1], // 將字串擷取得到數字年齡部分
                Integer::valueOf,          // 將上一步的年齡字串轉換成為Integer型別資料
                s -> s + 23);              // 將上一步的Integer資料加70,得到一個Integer結果
    }

    public static void convert(String s,
                               Function<String, String> fun1,
                               Function<String, Integer> fun2,
                               Function<Integer, Integer> fun3) {
        Integer i = fun1.andThen(fun2).andThen(fun3).apply(s);
        System.out.println("i = " + i);
    }
}
i = 523