Java - Use Functional Interfaces to code logics

Introduction

Gender enum contains two constants to represent the gender of a person.

Person class represents a person; it contains, apart from other methods, a getPersons() method that returns a list of persons.

The program does the following tasks:

  • gets a list of persons,
  • applies a filter to the list to get a list of only males,
  • maps persons to the year of their birth, and adds one year to each male's date of birth.

It performs each of these actions using lambda expressions.

Demo

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

enum Gender {//ww  w  .  ja  va2  s. c  o  m
  MALE, FEMALE
}

class Person {
  private String firstName;
  private String lastName;
  private LocalDate dob;
  private Gender gender;

  public Person(String firstName, String lastName, LocalDate dob, Gender gender) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.dob = dob;
    this.gender = gender;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public LocalDate getDob() {
    return dob;
  }

  public void setDob(LocalDate dob) {
    this.dob = dob;
  }

  public Gender getGender() {
    return gender;
  }

  public void setGender(Gender gender) {
    this.gender = gender;
  }

  @Override
  public String toString() {
    return firstName + " " + lastName + ", " + gender + ", " + dob;
  }

  // A utility method
  public static List<Person> getPersons() {
    ArrayList<Person> list = new ArrayList<>();
    list.add(new Person("A", "D", LocalDate.of(1975, 1, 20), Gender.MALE));
    list.add(new Person("B", "E", LocalDate.of(1965, 9, 12), Gender.MALE));
    list.add(new Person("C", "D", LocalDate.of(1970, 9, 12), Gender.FEMALE));
    return list;
  }
}

class FunctionUtil {
  // Applies an action on each item in a list
  public static <T> void forEach(List<T> list, Consumer<? super T> action) {
    for (T item : list) {
      action.accept(item);
    }
  }

  // Applies a filter to a list and returned the filtered list items
  public static <T> List<T> filter(List<T> list, Predicate<? super T> predicate) {
    List<T> filteredList = new ArrayList<>();
    for (T item : list) {
      if (predicate.test(item)) {
        filteredList.add(item);
      }
    }
    return filteredList;
  }

  // Maps each item in a list to a value
  public static <T, R> List<R> map(List<T> list, Function<? super T, R> mapper) {
    List<R> mappedList = new ArrayList<>();
    for (T item : list) {
      mappedList.add(mapper.apply(item));

    }
    return mappedList;
  }
}

public class Main {
  public static void main(String[] args) {
    List<Person> list = Person.getPersons();

    // Use the forEach() method to print each person in the list
    System.out.println("Original list of persons:");
    FunctionUtil.forEach(list, p -> System.out.println(p));

    // Filter only males
    List<Person> maleList = FunctionUtil.filter(list, p -> p.getGender() == Gender.MALE);

    System.out.println("\nMales only:");
    FunctionUtil.forEach(maleList, p -> System.out.println(p));

    // Map each person to his/her year of birth
    List<Integer> dobYearList = FunctionUtil.map(list, p -> p.getDob().getYear());

    System.out.println("\nPersons mapped to year of their birth:");
    FunctionUtil.forEach(dobYearList, year -> System.out.println(year));

    // Apply an action to each person in the list
    // Add one year to each male's dob
    FunctionUtil.forEach(maleList, p -> p.setDob(p.getDob().plusYears(1)));

    System.out.println("\nMales only after ading 1 year to DOB:");
    FunctionUtil.forEach(maleList, p -> System.out.println(p));
  }
}

Result

Related Topic