Sunday, September 20, 2015

Java 8 Lambda

Took the java8 case study posted from oracle and tried it out. It is a maven project with working code and unit test.

https://github.com/maheshrajannan/java8lamda

Please take a look at the java 8 lambda code below,

RoboContactLambda.java
       

 public List phoneContacts(List people, Predicate aTest) {
  List phoned = new ArrayList<>();
  for (Person person : people) {
   if (aTest.test(person)) {
    phoned.add(roboCall(person));
   }
  }
  return phoned;
 }

       
 

RoboContactLambdaTest.java
       

     //INFO: the age checks are not re-usable. They are repeated.
     driverPredicate = p -> p.getAge() >= 16;
     drafteePredicate = p -> p.getAge() >= 18
       && p.getAge() <= 25 && p.getGender() == Gender.MALE;
     pilotPredicate = p -> p.getAge() >= 23 && p.getAge() <= 65; 

       

RoboContactMyLambda.java
       

 public List<Person> phoneContacts(
           List<Person> people, MyPredicate<Person> aTest) {
  List<Person> phoned = new ArrayList<>();
  for (Person person : people) {
   if (aTest.test(person)) {
    phoned.add(roboCall(person));
   }
  }
  return phoned;
 }

       
 

RoboContactMyLambdaTest.java
       

     /**
      * Custom predicates...really Generic Classes 
             * much easier and more re-usable.
      */
     driverPredicate = DriverPredicate.getInstance();
     drafteePredicate = DrafteePredicate.getInstance();
     pilotPredicate = PilotPredicate.getInstance();

       

No comments:

Post a Comment