Java_Supplier接口的使用纪要

package cn.go.supplier;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;

/**
 * Java_Supplier接口的使用:
 *
 * Supplier接口是Java1.8之后的典型函数式接口;
 * 作用:
 *  1. 获取泛型参数指定类型的对象数据;
 *  2. 创建对象;
 * 说明: 由于Supplier是一个函数式接口,那也就是说Lambda表达式需要"对外提供"
 *  一个符合泛型类型的对象数据;
 *
 */
public class FistSupplier {

    /**
     * Lambda典型的封装方式;
     * @param function
     * @return
     */
    private static String getString(Supplier function) {

        // 对象数据返回;
        return function.get();
    }

    private static Student getStudent(Supplier supplier) {

        return supplier.get();
    }

    /**
     * Java主函数|入口;
     * @param args
     */
    public static void main(String[] args) {

        // 使用new关键字实例化对象;
        Student student = new Student();

        // 数据初始化;
        String a = "Hello";
        String b = "World";

        // 对象参数赋值;
        student.setNAME(a);
        student.setVALUE(b);

        /**
         * 1.
         * getString(() -> a):
         *  返回字符串对象数据;
         */
        String stra = getString(() -> a);

        /**
         * 2.
         * getStudent(() -> student):
         *  返回student对象数据;
         */
        Student student1 = getStudent(() -> student);

        /**
         * 3.
         * Supplier supplier = Student::new;
         *  创建既定类型Student类型的Supplier容器;
         */
        Supplier supplier = Student::new;

        /**
         * supplier.get();
         *  返回空的对象实例;
         * 说明:get()方法每调用一次都会返回新的对象;
         */
        Student student2 = supplier.get();

        // 对象参数赋值;
        student2.setVALUE(a);

        // 数据输出;
        System.out.println(stra);

        System.out.println(student1.getVALUE());

        System.out.println(student2.toString());
    }
}

class Student {

    private String NAME;

    private String VALUE;

    public Student() {};

    public Student(String NAME, String VALUE) {
        this.NAME = NAME;
        this.VALUE = VALUE;
    }

    public String getNAME() {
        return NAME;
    }

    public void setNAME(String NAME) {
        this.NAME = NAME;
    }

    public String getVALUE() {
        return VALUE;
    }

    public void setVALUE(String VALUE) {
        this.VALUE = VALUE;
    }

    @Override
    public String toString() {
        return "Student{" +
                "NAME='" + NAME + '\'' +
                ", VALUE='" + VALUE + '\'' +
                '}';
    }
}
package cn.go.supplier;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;

/**
 * Java_Supplier使用实例:
 *  1. 计算int数组中最大的值;
 */
public class FistSupplier {

    // 封装Lambda接口;
    private static int getMax(Supplier supplier) {

        // 数据对象返回;
        return supplier.get();
    }

    /**
     * Java主函数|入口;
     * @param args
     */
    public static void main(String[] args) {

        // 数据对象初始化;
        int [] arr = {1,3,5,6,8,0,88,99};

        // 变量初始化;
        int maxVal = 0;

        /**
         * maxVal赋值;
         * getMax()实现;
         */
        maxVal = getMax(() -> {

            /**
             * 实例化一个临时变量记录最大值;
             * 初始化默认arr[0]为最大值;
             * 依次进行比对;
             */
            int max = arr[0];

            // 数据循环比对;
            for (int i : arr) {

                // 数据比对;
                if (i > max) {

                    // 比对结果处理;
                    max = i;
                }
            }

            // 数据返回;
            return max;
        });

        // 数据打印;
        System.out.println(maxVal);
    }
}

 

你可能感兴趣的:(Lambda表达式,#,Java)