Interface Name | Description | Abstract Method |
---|---|---|
Supplier<T> | A supplier of a result (reference type) | T get() |
DoubleSupplier | A supplier of double value result | double getAsDouble() |
IntSupplier | A supplier of int value result | int getAsInt() |
LongSupplier | A supplier of long value result | long getAsLong() |
BooleanSupplier | A supplier of boolean value result | boolean getAsBoolean() |
There is no default or static method in supplier interface.
public class Applicant {
private String name;
private int yearsOfExperience;
private boolean hasJavaSkills;
private boolean hasPythonSkills;
Applicant(String name, int yearsOfExperience,
boolean hasJavaSkills, boolean hasPythonSkills){
this.name = name;
this.yearsOfExperience = yearsOfExperience;
this.hasJavaSkills = hasJavaSkills;
this.hasPythonSkills = hasPythonSkills;
}
public String getName() {
return name;
}
public boolean hasPythonSkills() {
return hasPythonSkills;
}
}
public class Main {
public static void main(String[] args) {
List<Applicant> applicantList = new ArrayList<>();
applicantList.add(new Applicant("Alice", 8, true, true));
applicantList.add(new Applicant("Bob", 3, false, true));
applicantList.add(new Applicant("Carol", 7, true, false));
applicantList.add(new Applicant("Dave", 5, false, true));
applicantList.add(new Applicant("Eve", 6, true, true));
Supplier<List<Applicant>> supplier = () -> applicantList;
Predicate<Applicant> searchPredicate = (p) -> p.getName().equals("Bob") && p.hasPythonSkills();
boolean found = isApplicantPresent(supplier, searchPredicate);
System.out.println("Have you found Bob: " + found);
}
private static boolean isApplicantPresent(Supplier<List<Applicant>> supplier, Predicate<Applicant> predicate){
for(Applicant applicant: supplier.get()) {
if (predicate.test(applicant))
return true;
}
return false;
}
}