Supplier
源码
/**
* Represents a supplier of results.
*
* <p>There is no requirement that a new or distinct result be returned each
* time the supplier is invoked.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #get()}.
*
* @param <T> the type of results supplied by this supplier
*
* @since 1.8
*/
@FunctionalInterface
public interface Supplier<T> {
//不接收任何参数,返回一个结果
T get();
}
栗子:
public class SupplierTest {
public static void main(String[] args) {
Supplier<String> S1=()-> "摇一摇";//不接收参数,返回一个对象
System.out.println(S1.get());
Supplier<Student> S2=()-> new Student();//不接收参数,返回一个对象
System.out.println("name: "+S2.get().getName());
System.out.println("age: "+S2.get().getAge());
Supplier<Student> S3=Student::new;//不接收参数,返回一个对象
System.out.println("name: "+S3.get().getName());
System.out.println("age: "+S3.get().getAge());
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Student{
private String name="syc";
private int age=18;
}
console:
摇一摇
name: syc
age: 18
name: syc
age: 18
Process finished with exit code 0
BinaryOperator
BinaryOperator继承了 BiFunction
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
/**
* Returns a {@link BinaryOperator} which returns the lesser of two elements
* according to the specified {@code Comparator}.
*
* @param <T> the type of the input arguments of the comparator
* @param comparator a {@code Comparator} for comparing the two values
* @return a {@code BinaryOperator} which returns the lesser of its operands,
* according to the supplied {@code Comparator}
* @throws NullPointerException if the argument is null
*/
public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
}
/**
* Returns a {@link BinaryOperator} which returns the greater of two elements
* according to the specified {@code Comparator}.
*
* @param <T> the type of the input arguments of the comparator
* @param comparator a {@code Comparator} for comparing the two values
* @return a {@code BinaryOperator} which returns the greater of its operands,
* according to the supplied {@code Comparator}
* @throws NullPointerException if the argument is null
*/
public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
}
}
栗子:
public class SupplierTest {
public static void main(String[] args) {
System.out.println(calculate(1,2,(a,b)->a+b)); //3
}
public static int calculate(Integer a,Integer b,BinaryOperator<Integer> bi){
return bi.apply(a,b); //调用Bifunction的apply
}
}
minBy
静态方法:参数是一个Comparator,返回值是个BinaryOperator,即用(a, b) -> comparator.compare(a, b) <= 0 ? a : b实现BinaryOperator(Bifunction)的apply
栗子:
public class SupplierTest {
public static void main(String[] args) {
System.out.println(calculate(1,2,(a,b)->a+b)); //3
//获取小的
System.out.println(sort(1,2,(a,b)->a-b)); //1
//获取大的
System.out.println(sort(1,2,(a,b)->b-a)); //2
}
public static int calculate(Integer a,Integer b,BinaryOperator<Integer> bi){
return bi.apply(a,b); //调用Bifunction的apply
}
public static int sort(Integer a,Integer b,Comparator<Integer> C){
return BinaryOperator.minBy(C).apply(a,b);
}
}
maxBy
同minBy
给个饭钱?
- Post link: http://sovzn.github.io/2022/09/04/Supplier/
- Copyright Notice: All articles in this blog are licensed under unless otherwise stated.
若没有本文 Issue,您可以使用 Comment 模版新建。
GitHub Issues