函数式接口的基本用法
public class Demo {
public static void main(String[] args) {
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
consumer(new Consumer<Double>() {
@Override
public void accept(Double aDouble) {
System.out.println("吃饭泡妞花钱:"+aDouble);
}
},1000D);
consumer(s->System.out.println("喝酒吃放泡妞花钱:"+s),2000D);
System.out.println("=====================Supplier<T>供给型接口===================");
int[] numbers = getNumbers(() -> new Random().nextInt(100), 10);
System.out.println(Arrays.toString(numbers));
System.out.println("=====================Function<T>函数式接口===================");
int s1 = function(s -> s += 1, 1);
System.out.println(s1);
System.out.println("===================== Predicate<T>断言型接口===================");
List list1 = predicate(s -> s > 2, list);
System.out.println(list1);
}
public static void consumer(Consumer<Double> con,Double monery){
con.accept(monery);
}
public static int[] getNumbers(Supplier<Integer> sup,int count){
int[] arr = new int[count];
for (int i = 0; i < arr.length; i++) {
arr[i] = sup.get();
}
return arr;
}
public static int function(Function<Integer , Integer> fun,Integer s){
return fun.apply(s);
};
public static List<Integer> predicate(Predicate<Integer> pre,List<Integer> list){
List list1 = new ArrayList();
for (Integer s:list) {
if (pre.test(s)){
list1.add(s);
}
}
return list1;
};
}