先引入实例中需要的实体类,实体类中的get/set方法省略掉了
public class State { private String name; private String code; private String region; private Set<City> mainCities=new HashSet<City>(); public State(){} public State(String name){ this.name=name; } }
public class City { private String name; private String zipCode; private Integer population; private String climate; private double averageRainfall; public City(){} public City(String name){ this(name,null,null); } public City(String name,String zipCode,Integer population){ this.name=name; this.zipCode=zipCode; this.population=population; } }
public class StateToCityString implements Function<State,String> { @Override public String apply(State input) { return Joiner.on(",").join(input.getMainCities()); } }
Function TestCase:
public class FunctionTest { @Test public void test(){ Map<String,State> stateMap=new HashMap<String,State>(); State s=new State("New York"); s.addCity("new"); s.addCity("york"); stateMap.put("NY",s); //Functions.forMap 接收一个Map集合作为参数,返回一个Function,用于执行Map集合的查找 Function<String,State> lookup= Functions.forMap(stateMap); Function<State,String> stateFunction=new StateToCityString(); //Function<A, C> compose(Function<B, C> g, Function<A, ? extends B> f) //接收两个Function作为参数,返回两个Function的组合 //f的输出会作为g的输入,g输出为最终作为compose的输出 Function<String,String> composed=Functions.compose(stateFunction,lookup); String str=composed.apply("NY"); System.out.println(str); } }
Predicate
先来几个Predicate接口的实现:
public class LowRainfallPredicate implements Predicate<City> { @Override public boolean apply(City input) { return input.getAverageRainfall()<45.7; } }
public class PopulationPredicate implements Predicate<City> { @Override public boolean apply(City input) { return input.getPopulation()<=50000; } }
public class TemperateClimatePredicate implements Predicate<City>{ @Override public boolean apply(City input) { return input.getClimate().equals("gogo"); } }
public class SouthwestOrMidwestRegionPredicate implements Predicate<State> { @Override public boolean apply(State input) { return input.getRegion().equals("MIDEWEST")||input.getRegion().equals("SOUTHWEST"); } }
public class PredicateDemo { @Test public void test(){ TemperateClimatePredicate temperateClimatePredicate=new TemperateClimatePredicate(); LowRainfallPredicate lowRainfallPredicate=new LowRainfallPredicate(); PopulationPredicate smallPopulationPredicate=new PopulationPredicate(); //Predicates.and Predicate smallAndDry= Predicates.and(smallPopulationPredicate,lowRainfallPredicate); //Predicates.or Predicate smallTemperate=Predicates.or(smallPopulationPredicate,temperateClimatePredicate); //Predicates.not Predicate largeCityPredicate=Predicates.not(smallPopulationPredicate); //Predicates.compose //Predicate<A> compose(Predicate<B> predicate, Function<A, ? extends B> function) //底层调用:predicate.apply(function.apply(A)) Map<String,State> stateMap=new HashMap<String,State>(); State s=new State("New York"); s.setRegion("MIDEWEST"); s.addCity("new"); s.addCity("york"); stateMap.put("NY",s); Function<String,State> lookup= Functions.forMap(stateMap); SouthwestOrMidwestRegionPredicate southwestOrMidwestRegionPredicate=new SouthwestOrMidwestRegionPredicate(); Predicate<String> predicate=Predicates.compose(southwestOrMidwestRegionPredicate,lookup); System.out.println(predicate.apply("NY")); } }