2018-02-07

package com.richard.lambdaexpressions;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class RosterTest {
interface CheckPerson{
boolean test(Person p);
}

//approach 1:Create Methods that search for members that match one characteristic
public static void printPersonsOlderThan(List roster,int age){
    
    for(Person p:roster){
        if(p.getAge()>=age){
            p.printPerson();
        }
    }
}

//approach 2:Create more generalized search methods
public static void printPersonsWithAgeRange(List roster,int low,int high){
    
    for(Person p:roster){
        
        if(low<=p.getAge()&& p.getAge()<=high){
            
            p.printPerson();
        }       
    }
}

//Approach 3:specify search criteria code in a local class
public static void printPersons(List roster,CheckPerson tester){
    
    for(Person p:roster){
        
        if(tester.test(p)){
            
            p.printPerson();
        }
    }   
}

//approach 6:use standard functional interfaces with lambda expressions
public static void printPersonsWithPredicate(Listroster,Predicate tester){
    
    for(Person p:roster){
        
        if(tester.test(p)){
            
            p.printPerson();
        }
    }
}

//approach 7: Use Lambda Expressions throughout your application.
public static void processPersons(Listroster,
        Predicatetester,Consumerblock){
    for(Person p:roster){
        if(tester.test(p)){
            block.accept(p);
        }
    }
}

public static void processPersonWithFunction(
        List roster,
        Predicate tester,
        Function mapper,
        Consumer block){
    for(Person p:roster){
        
        if(tester.test(p)){
            
            String data=mapper.apply(p);
            block.accept(data);
        }
    }
}

//Approach 8:use generics more extensively
public static  void processElements(
        Iterable source,
        Predicate tester,
        Function mapper,
        Consumer block){
    
    for(X p:source){
        
        if(tester.test(p)){
            
            Y data=mapper.apply(p);
            block.accept(data);
        }
    }
}


public static void main(String... args){
    
    List roster=Person.createRoster();
    for(Person p:roster){
        p.printPerson();
    }
    System.out.println("********approach 1***************");
    //approach 1:Create Methods that search for members that match one characteristic
    printPersonsOlderThan(roster,30);
    
    System.out.println("********approach 2***************");
    //approach 2:Create more generalized search methods
    printPersonsWithAgeRange(roster,20,30);
    
    System.out.println("********approach 3***************");
    //approach 3: specify search criteria code in a local class
    class CheckPersonEligibleForSelectiveService implements CheckPerson{
        
        public boolean test(Person p){
            
            return p.getGender()==Person.Sex.MALE && 
                    p.getAge()>=20 &&
                    p.getAge()<=30;
        }
    }
    printPersons(roster,new CheckPersonEligibleForSelectiveService());
    
    System.out.println("********approach 4***************");
    //approach 4:specify search criteria code in an anonymous class
    
    printPersons(roster,new CheckPerson(){
        
        public boolean test(Person p){
            
            return p.getGender()==Person.Sex.MALE &&
                    p.getAge()>=18 &&
                    p.getAge()<=30;
        }
        
    });
    
    System.out.println("********approach 5***************");
    //approach 5:Specify search criteria code with a lambda expression
    printPersons(roster,(Person p)->p.getGender()==Person.Sex.MALE &&
            p.getAge()>=18 &&
            p.getAge()<=30);
    
    
    System.out.println("********approach 6***************");
    //approach 6:Use standard functional interfaces with lambda expressions
    printPersonsWithPredicate(roster,(Person p)->p.getGender()==Person.Sex.MALE &&
            p.getAge()>=18 &&
            p.getAge()<=30);
    
    System.out.println("********approach 7***************");
    //approach 7:Use Lambda Expressions throughout your application.
    processPersons(roster,
            p->p.getGender()==Person.Sex.MALE &&
            p.getAge()>=20 &&
            p.getAge()<=30,
            p->p.printPerson());
    
    processPersonWithFunction(roster,
            p->p.getGender()==Person.Sex.MALE &&
            p.getAge()>=20 &&
            p.getAge()<=30,
            p->p.getEmailAddress(),
            email->System.out.println(email));
    
    System.out.println("********approach 8***************");
    //Approach 8:Use Generics More Extensively
    processPersons(roster,
            p->p.getGender()==Person.Sex.MALE &&
            p.getAge()>=20 &&
            p.getAge()<=30,
            p->p.printPerson());
    
    processElements(
            roster,
            p->p.getGender()==Person.Sex.MALE &&
            p.getAge()>=20 &&
            p.getAge()<=30,
            p->p.getEmailAddress(),
            email->System.out.println(email));
    
    System.out.println("********approach 9***************");
    //Aproach 9: use aggregate operations that accept Lambda Expressions as parameters
            
    roster
    .stream()
    .filter(
            p->p.getGender()==Person.Sex.MALE &&
            p.getAge()>=20 &&
            p.getAge()<=30)
    .map(p->p.getEmailAddress())
    .forEach(email->System.out.println(email));
}

}

你可能感兴趣的:(2018-02-07)