WebFlux响应式编程

Posted by Chenyawei on 2021-01-29
Words 1.4k and Reading Time 7 Minutes
Viewed Times

一、函数编程

1、概念

命令式编程:关注怎么样做

函数式编程:关注做什么,lambda, stream,代码更加的简短

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class GetMinV {
public static void main(String[] args) {
int nums[] = {11,43,44,55,66,22,35,94,80};
// 命令式编程
int min = Integer.MAX_VALUE;
for (int num : nums) {
if (num < min){
min = num;
}
}
System.out.println(min);
// 函数式编程,parallel() 并行处理
int asInt = IntStream.of(nums).parallel().min().getAsInt();
System.out.println(asInt);
}
}
1
2
3
4
5
6
7
8
9
10
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("ok");
}
}).start();
// 代码简短
new Thread(()->System.out.println("ok")).start();
}

2、lambda

函数接口 , 默认方法(带默认实现的方法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class LambdaDemo {
public static void main(String[] args) {
Interface1 interface1 = (i) -> i * 2;
System.out.println(interface1.doubleNum(8));
System.out.println(interface1.add(9,8));
// 最常用的写法
Interface1 interface12 = i-> i*3;
Interface1 interface13 = (int i)->i*4;
Interface1 interface14 = (int i) -> {
System.out.println("33");
return i*5;
};
}
}
// 尽可能一个接口只做一件事件
@FunctionalInterface
interface Interface1{
int doubleNum(int i);
default int add(int i,int j){
return i+j;
}
}

3、jdk8 中重要和常用的函数接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
interface IMoneyFormat{
String format(int i);
}
class MyMoney {
private final int money;
public MyMoney(int money) {
this.money = money;
}
public void printMoney(IMoneyFormat iMoneyFormat){
System.out.println("我的存款:"+ iMoneyFormat.format(this.money));
}
}
public class MoneyDemo{
public static void main(String[] args) {
MyMoney myMoney = new MyMoney(999999999);
myMoney.printMoney(i -> new DecimalFormat("#,###").format(i));
}
}

lambda 表达式不关心接口名和方法名是什么,只关心输入是什么,输出是什么, 所以可以省略IMoneyFormat接口,用jdk8自带的接口Function<>

  • Function

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    interface IMoneyFormat{
    String format(int i);
    }
    class MyMoney {
    private final int money;
    public MyMoney(int money) {
    this.money = money;
    }
    public void printMoney(Function<Integer, String> iMoneyFormat){
    System.out.println("我的存款:"+ iMoneyFormat.apply(this.money));
    }
    }
    public class MoneyDemo{
    public static void main(String[] args) {
    MyMoney myMoney = new MyMoney(999999999);
    myMoney.printMoney(i -> new DecimalFormat("#,###").format(i));
    }

    Function<Integer, String> integerStringFunction = i -> new DecimalFormat("#,###").format(i);
    // 函数链式操作,操作更加灵活
    myMoney.printMoney(integerStringFunction.andThen(s -> "人民币" + s));
    }
  • Predicate

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class FunctionInterface {
    public static void main(String[] args) {
    // 断言函数接口
    Predicate<Integer> predicate = i-> i > 0;
    System.out.println(predicate.test(-9));
    IntPredicate predicate1 = i-> i>9;
    System.out.println(predicate1.test(99));
    // 消费函数接口
    Consumer<String> consumer = s -> System.out.println(s);
    consumer.accept("输入的内容");
    }
    }
  • Consumer

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public class DemoPrintInfo {
    public static void main(String[] args) {
    String[] array = { "大雄,男", "静香,女", "胖虎,男" };
    printInfo(
    s -> System.out.print("姓名:" + s.split(",")[0] + ","),
    s -> System.out.println("性别:" + s.split(",")[1] + "。"),
    array
    );
    }

    private static void printInfo(Consumer<String> one, Consumer<String> two, String[] array) {
    for (String info : array) {
    one.andThen(two).accept(info);
    }
    }
    }

    输出:
    姓名:大雄,性别:男。
    姓名:静香,性别:女。
    姓名:胖虎,性别:男。
  • Supplier

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    public class FunctionInterface {
    public static void main(String[] args) {
    String msgA = "Hello ";
    String msgB = "World ";
    System.out.println(
    getString(
    () -> msgA + msgB
    )
    );
    }
    private static String getString(Supplier<String> stringSupplier) {
    return stringSupplier.get();
    }
    }

    // 数组中的最大值
    public class DemoNumberMax {
    public static void main(String[] args) {
    int[] numbers = {100, 200, 300, 400, 500, -600, -700, -800, -900, -1000};
    int numberMax = arrayMax(
    () -> {
    int max = numbers[0];
    for (int number : numbers) {
    if (max < number) {
    max = number;
    }
    }
    return max;
    }
    );
    System.out.println("数组中的最大值为:" + numberMax);
    }

    /**
    * 获取一个泛型参数指定类型的对象数据
    * @param integerSupplier 方法的参数为Supplier,泛型使用Integer
    * @return 指定类型的对象数据
    */
    public static Integer arrayMax(Supplier<Integer> integerSupplier) {
    return integerSupplier.get();
    }
    }
  • UnaryOperator 一元函数输入输出内容相同

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static void main(String[] args) {
    UnaryOperator<Integer> operator1 = t -> t + 10;
    UnaryOperator<Integer> operator2 = t -> t * 10;
    // Using andThen() 最后执行andThen函数;返回一个组合函数,该函数首先将此函数应用于它的输入,然后将{@code after}函数应用于结果。如果对任一函数的求值抛出异常,则将其中继到组成函数的调用者。
    int a = operator1.andThen(operator2).apply(5);
    System.out.println(a);
    // Using compose() 先执行compose函数;返回一个组合函数,该函数首先应用{@code before}函数对其输入进行运算,然后将此函数应用于结果。如果对任一函数的求值抛出异常,则将其中继到组成函数的调用者。
    int b = operator1.compose(operator2).apply(5);
    System.out.println(b);
    }
  • BiFunction 两个输入函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class FunctionInterface{
    public static void main(String[] args) {
    System.out.println(getSum(1,2,(a,b)->a+b));
    System.out.println(getSum(1,2,(a,b)->a-b));
    System.out.println(getSum(1,2,(a,b)->a*b));
    System.out.println(getSum(2,2,(a,b)->a/b));
    }
    public static Integer getSum(Integer a, Integer b, BiFunction<Integer, Integer, Integer> biFunction{
    return biFunction.apply(a, b);
    }
    }
  • BinaryOperator 二元函数输入输出类型相同

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    public class FunctionInterface{
    public static void main(String[] args){
    testBinaryOperator(1,2,(num1,num2)->num1 + num2);
    testMinBy("hello","wonders",(str1,str2)->str1.length()-str2.length());
    //方法引用
    testMinBy("hello","wonders", Comparator.comparingInt(String::length));
    testMinBy("hello","wonders",(str1,str2)->str1.charAt(0)-str2.charAt(0));
    //方法引用
    testMinBy("hello","wonders", Comparator.comparingInt(str -> str.charAt(0)));
    testMaxBy("hello","wonders",(str1,str2)->str1.length()-str2.length());
    //方法引用
    testMaxBy("hello","wonders", Comparator.comparingInt(String::length));
    testMaxBy("hello","wonders",(str1,str2)->str1.charAt(0)-str2.charAt(0));
    //方法引用
    testMaxBy("hello","wonders", Comparator.comparingInt(str -> str.charAt(0)));
    }
    public static void testBinaryOperator(Integer num1, Integer num2, BinaryOperator<Integer> result){
    System.out.println(result.apply(num1,num2));
    }
    /**
    * 返回两者里面较小的一个
    * @param str1
    * @param str2
    * @param comparator
    */
    public static void testMinBy(String str1, String str2, Comparator<String> comparator){
    System.out.println(BinaryOperator.minBy(comparator).apply(str1,str2));
    }
    /**
    * 返回两者里面较大的一个
    * @param str1
    * @param str2
    * @param comparator
    */
    public static void testMaxBy(String str1, String str2, Comparator<String> comparator){
    System.out.println(BinaryOperator.maxBy(comparator).apply(str1,str2));
    }
    }

notice

欢迎访问 chenyawei 的博客, 若有问题或者有好的建议欢迎留言,笔者看到之后会及时回复。 评论点赞需要github账号登录,如果没有账号的话请点击 github 注册, 谢谢 !

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !