1 package LambdaTest.LambdaTest04;
2
3 import java.util.function.Supplier;
4
5 /**
6 * FileName: SupplierTest
7 * Author: lps
8 * Date: 2022/4/5 16:51
9 * Sign:刘品水 Q:1944900433
10 *
11 * @FunctionalInterface public interface Supplier<T>
12 * 表示结果的供应商。
13 * 没有要求一个新的或不同的结果被返回时,每个供应商被调用。
14 * <p>
15 * 这是一个functional interface其功能的方法是get()。
16 */
17 public class SupplierTest {
18 public static void main(String[] args) {
19 // String s=getstring(()->{
20 // return "刘品水";
21 // });
22 String s = getstring(() -> "刘品水");
23 System.out.println(s);
24
25 Integer i = getInteger(() -> 22);
26 System.out.println(i);
27
28
29 int[] arr = {123, 112, 186, 157, 35};
30 int MaxNumber = getMax(() -> {
31 int max = arr[0];
32 for (int j = 0; j < arr.length; j++) {
33 if (max<arr[j]){
34 max=arr[j];
35 }
36 }
37 return max;
38 });
39 System.out.println(MaxNumber);
40
41 }
42
43 //定义一个方法 返回一个字符串数据源
44 private static String getstring(Supplier<String> sup) {
45 return sup.get();
46 }
47
48 //@FunctionalInterface
49 //public interface Supplier<T> {
50 //
51 // /**
52 // * Gets a result.
53 // *
54 // * @return a result
55 // */
56 // T get();
57 //}
58 private static Integer getInteger(Supplier<Integer> sup) {
59 return sup.get();
60 }
61
62 private static int getMax(Supplier<Integer> sup) {
63 return sup.get();
64 }
65
66 }
![]()