05.java函数式编程

1.Java 泛型的正常使用

泛型有三种使用方式,分别为:泛型类、泛型接口、泛型方法

1.1java 中泛型标记符

  •    E - Element (在集合中使用,因为集合中存放的是元素)   
  •    T - Type(表示Java 类,包括基本的类和我们自定义的类)
  •    K - Key(表示键,比如Map中的key)
  •    V - Value(表示值)
  •    ? - (表示不确定的java类型)

1.2java中泛型方法

方法定义: 

public <T……> T method(T t1……)

可见性: 泛型声明 :返回值: 方法名(泛型参数)

查看代码
 public class javaFunTestMethod {
    /*
    * 泛型比较方法
    * */
    public static <T extends Comparable<T>> T maximum(T x, T y, T z)
    {
        T max = x; // 假设x是初始最大值
        if ( y.compareTo( max ) > 0 ){
            max = y; //y 更大
        }
        if ( z.compareTo( max ) > 0 ){
            max = z; // 现在 z 更大
        }
        return max; // 返回最大对象
    }
    // 泛型数组处理方法 printArray
    public static < E > void printArray( E[] inputArray )
    {
        // 输出数组元素
        for ( E element : inputArray ){
            System.out.printf( "%s ", element );
        }
        System.out.println();
    }
}

测试用例:

@SpringBootTest(classes = XbxleetcodeApplication.class)
public class javaFunMethodTest {
    JavaFunTestMethod javaFunTestMethod=new JavaFunTestMethod();
    @Test
     public void funTest() {
        //比较数据的大小
        System.out.printf( "%d, %d 和 %d 中最大的数为 %d\n\n",
                3, 4, 5, javaFunTestMethod.maximum( 3, 4, 5 ) );

        System.out.printf( "%.1f, %.1f 和 %.1f 中最大的数为 %.1f\n\n",
                6.6, 8.8, 7.7, javaFunTestMethod.maximum( 6.6, 8.8, 7.7 ) );

        System.out.printf( "%s, %s 和 %s 中最大的数为 %s\n","pear",
                "apple", "orange", javaFunTestMethod.maximum( "pear", "apple", "orange" ) );

        /*
        * 对数组的使用的处理
        * */
        // 创建不同类型数组: Integer, Double 和 Character
        Integer[] intArray = { 1, 2, 3, 4, 5 };
        Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
        Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
        javaFunTestMethod.printArray(intArray);
        javaFunTestMethod.printArray(doubleArray);
        javaFunTestMethod.printArray(charArray);
    }
}

执行结果:

3, 4 和 5 中最大的数为 5

6.6, 8.8 和 7.7 中最大的数为 8.8

pear, apple 和 orange 中最大的数为 pear
1 2 3 4 5 
1.1 2.2 3.3 4.4 
H E L L O 

1.3java中泛型类

方法定义:class 类名 <T>{} 

查看代码
/*
* 泛型类
* */
public  class Box<T> {

    private T t;

    public void add(T t) {
        this.t = t;
    }

    public T get() {
        return t;
    }
}

测试用例:

@Test
public void fanTClassTest(){
    Box<Integer> integerBox = new Box<Integer>();
    Box<String> stringBox = new Box<String>();

    integerBox.add(new Integer(10));
    stringBox.add(new String("菜鸟教程"));

    System.out.printf("整型值为 :%d\n\n", integerBox.get());
    System.out.printf("字符串为 :%s\n", stringBox.get());
}

执行结果:

整型值为 :10

字符串为 :菜鸟教程

1.4java中泛型接口

方法定义:interface 类名<T>{} 

1.5java中泛型限制

方法定义: 上限,下限

上限使用extends,表示参数类型只能是该类型或该类型的子类。
泛型对象的下限使用super,表示参数类型只能是该类型或该类型的父类。

/*
 * 上限使用extends,表示参数类型只能是该类型或该类型的子类
 * */
public static void getUperNumber(List<? extends Number> data) {
    System.out.println("data :" + data.get(0));
}
/*
* 泛型对象的下限使用super,表示参数类型只能是该类型或该类型的父类
* */
public static void getData(List<? super Number> data) {
    System.out.println("data :" + data.get(0));
}

2.函数式接口编程 

参考文档:https://blog.csdn.net/qq1309664161/article/details/128328202

函数式接口
一个接口中,有且只有一个抽象方法,这个接口就叫做函数式接口。常常使用@FunctionalInterface注解作为编译校验。满足函数式接口的要求,才能校验通过,否则会在校验阶段失败。接口中有且只能有一个抽象方法,那么非默认方法,如default修饰的方法,可以有多个,这个没有限制。函数式接口的作用就是函数式编程的入口,只有在函数式接口的抽象方法处写lamada表达式的函数式编程,java才能识别。所以说,函数式接口的出现就是为了Java能够使用函数式编程,而函数式编程的好处就是写法简单。这就是Java为何要引入函数式接口的意义。

2.1java中理解函数式编程

Java倡导“一切皆对象”,面向对象编程(OOP)是对数据进行抽象,主要抽象类/对象,是命令式编程风格。而函数式编程(OOF)是对行为进行抽象,主要是抽象函数,是声明式编程风格,其核心是:在思考问题时,使用不可变值和函数,函数对一个值进行处理,映射成另一个值。

2.2Lambda表达式使用方式

public void funTest() {
        /**
         * 核心函数式接口
         */
// Consumer
        Consumer<String> consumer = str -> System.out.println(str);
        consumer.accept("Consumer-消费型,有入参无返回");
// Supplier
        Supplier<String> supplier = () -> "Supplier-供给型,无入参有返回";
        System.out.println(supplier.get());
// Function
        Function<Integer, String> function = integer -> {
            if (integer > 0) {
                return "入参大于0";
            }
            return "入参小于等于0";
        };
        System.out.println("Function-函数市接口,有入参有返回" + function.apply(-10));
// Predicate
        Predicate<Integer> predicate = integer -> integer < 10;
        System.out.println("Predicate-断言型接口,有入参有返回" + predicate.test(20));

/**
 * 扩展函数式接口
 */
// BiFunction
        BiFunction<Integer, Integer, Integer> biFunction = (x, y) -> x * y;
    }

2.3函数式接口

public <T……> T method(T t1……)

可见性: 泛型声明 :返回值: 方法名(泛型参数)

经典案例:根据集合进行分片执行调用流程

/**
 * 集合接口分片执行
 */
public static <E, R> List<R> splitListFunction(List<E> codes, int splitSize, Function<List<E>, List<R>> fn) {
    List<R> resList = new ArrayList<>();
    for (int i = 0; i < codes.size(); i += splitSize) {
        //获取到入参
        List<E> subList = codes.subList(i, Math.min(i + splitSize, codes.size()));
        //使用函数式编程进行正常的调用
        List<R> res = fn.apply(subList);
        resList.addAll(res);
    }
    return resList;
}

测试案例:

@Test
public void  funSplitListFunctionTest(){
    List<String> pids=new ArrayList<>();
    Random r=new Random();
    Integer randomNum=0;
    for(int i=0;i<100;i++){
        randomNum=r.nextInt();
        pids.add(randomNum.toString());
    }
    //
    System.out.println(pids);
    Function<List<String>, List<Integer>> pidInfoGetter =
             inputList -> selectProductBaseInfo(inputList);

    List<Integer> stringList=JavaFunTestMethod.splitListFunction(pids,5,pidInfoGetter);
    System.out.println(stringList);
}

private List<Integer> selectProductBaseInfo(List<String> codes){
    if(!CollectionUtils.isEmpty(codes)){
        return codes.stream().map(o->Integer.parseInt(o)).collect(Collectors.toList());
    }
    return null;
}

执行结果:

随机数执行:

[1511537386, 1452721319, -420695640, 1017275181, -2074017590, -1140743621, -1584067420, 969695295, 248363757, 561471173, 2079117699, 1324558941, -2064970313, -623629615, -1697593901, 1538232383, -1805966675, -2119730986, 1606202755, -915684290, -1500418433, 566158579, -569650555, -1326879449, -1451863224, 961242075, 871164591, -1999146927, -1691606253, 95808780, 1372166114, 1529989360, -1195161951, -170388410, -2060595603, -269025108, 943950952, -1935653317, -1792053837, -2140922515, -2008226849, 2070977666, 741062410, 1201739896, -84801288, 1828879569, -1750413594, -506121537, 445899758, 2139649899, -510153674, -1304708492, 1185718592, 443226327, -537060851, 558306782, 2011547132, -392912174, -396447867, 644425584, -1899707328, -955588475, 1406067167, 1257594647, 2062406653, 1473612282, 1330746607, -972944173, 45421566, 1176693452, -1825780298, -244070263, 1739732776, -1182547938, 1908370256, -1346960618, -2054471165, -969768498, 1167067664, 1163465895, 141499512, 384404201, 214201577, 452156487, -1325335588, -1928407712, 139741455, 401622201, 1208084781, 221461756, 1968156637, 651903623, 1695934362, 994999695, 146625273, -1519771814, -975798456, 453009515, 958459573, 1182738060]

随机数泛型方法执行之后的方法
[1511537386, 1452721319, -420695640, 1017275181, -2074017590, -1140743621, -1584067420, 969695295, 248363757, 561471173, 2079117699, 1324558941, -2064970313, -623629615, -1697593901, 1538232383, -1805966675, -2119730986, 1606202755, -915684290, -1500418433, 566158579, -569650555, -1326879449, -1451863224, 961242075, 871164591, -1999146927, -1691606253, 95808780, 1372166114, 1529989360, -1195161951, -170388410, -2060595603, -269025108, 943950952, -1935653317, -1792053837, -2140922515, -2008226849, 2070977666, 741062410, 1201739896, -84801288, 1828879569, -1750413594, -506121537, 445899758, 2139649899, -510153674, -1304708492, 1185718592, 443226327, -537060851, 558306782, 2011547132, -392912174, -396447867, 644425584, -1899707328, -955588475, 1406067167, 1257594647, 2062406653, 1473612282, 1330746607, -972944173, 45421566, 1176693452, -1825780298, -244070263, 1739732776, -1182547938, 1908370256, -1346960618, -2054471165, -969768498, 1167067664, 1163465895, 141499512, 384404201, 214201577, 452156487, -1325335588, -1928407712, 139741455, 401622201, 1208084781, 221461756, 1968156637, 651903623, 1695934362, 994999695, 146625273, -1519771814, -975798456, 453009515, 958459573, 1182738060]

 

2.4 常用的函数式接口

posted @ 2023-02-08 14:58  冰融心  阅读(76)  评论(0编辑  收藏  举报