TechnologyZer
technologyzer.com

Introduction to Supplier Functional Interface

get() is an abstruct method in Supplier functional interface. It supplies a result of type T.

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;
    }
}

Leave a Comment