函数式编程:
使用Function接口(jdk8中已经存在):
/**
* 其功能就是将输入类型转换为输出类型
*/
public interface Function<F, T> {
T apply(@Nullable F input);
}
比如一个简单的日期转换:
/**
* 日期转换
*/
public class DateFormatFunction implements Function<Date, String> {
@Override
public String apply(Date input) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
return dateFormat.format(input);
}
}
使用Functions类:
Functions.forMap()方法:
/** * 州类 */ public class State { private String name; private String code; private Set
mainCities = new HashSet (); }
现在你想在一个Map<String, State>(key为州的编号)对象中查找某个key, 你可以:
Map<String, State> states = new HashMap<String, State>();
Function<String, State> lookup = Functions.forMap(states);
System.out.println(lookup.apply(key));//key不存在会抛异常
//你也可以给不存在的key指定一个默认值
Function<String, State> lookup = Functions.forMap(states, null);
Functions.compose()方法
/城市类/ public class City { private String name; private String zipCode; private int population; @Override public String toString() { return name; } }
/** * 将州的城市转换为字符串 */ public class StateToCityString implements Function<State, String> { @Override public String apply(State input) { return Joiner.on(",").join(input.getMainCities()); } }
你可以通过组合Function,查找某州的城市列表
Function<String, State> lookup = Functions.forMap(states);
Function<State, String> stateFunction = new StateToCityString(); //州到城市的转换
Function<String, String> stateCitiesFunction = Functions.compose(stateFunction, lookup); //组合Function
System.out.println(stateCitiesFunction.apply(key));
等价于:
stateFunction.apply(lookup.apply(key));
使用Predicate接口(jdk8中已存在):
Predicate接口
public interface Predicate
{ boolean apply(T input); //不同于Function.apply, 该apply用于过滤对象 }
如:
/**
* 过滤人口小于500000的城市
*/
public class PopulationPredicate implements Predicate<City> {
@Override
public boolean apply(City input) {
return input.getPopulation() <= 500000;
}
}
使用Predicates类:
有两个过滤条件:
/**
* 选择气候为TEMPERATE的城市
*/
public class TemperateClimatePredicate implements Predicate<City> {
@Override
public boolean apply(City input) {
return input.getClimate().equals(Climate.TEMPERATE);
}
}
/**
* 选择雨量小于45.7的城市
*/
public class LowRainfallPredicate implements Predicate<City> {
@Override
public boolean apply(City input) {
return input.getAverageRainfall() < 45.7;
}
}
你可以运用下面的方法实现过滤组合等:
Predicates.and(smallPopulationPredicate,lowRainFallPredicate);//且
Predicates.or(smallPopulationPredicate,temperateClimatePredicate);//或
Predicate.not(smallPopulationPredicate);//非
Predicates.compose(smallPopulationPredicate,lookup);//组合转换再过滤
使用Supplier接口:
Supplier接口
public interface Supplier
{ T get(); //用于创建对象 }
使用Suppliers类:
Suppliers.memorize()方法:
SupplyCity sc = new SupplyCity(); System.out.println(Suppliers.memoize(sc).get()); System.out.println(Suppliers.memoize(sc).get());//返回同一对象, 单例
Suppliers.memorizeWithExpiration()方法:
SupplyCity sc = new SupplyCity(); //超时再新建对象, 类似缓存 Supplier
supplier = Suppliers.memoizeWithExpiration(sc, 5, TimeUnit.SECONDS); City c = supplier.get(); System.out.println(c); Thread.sleep(3000); c = supplier.get(); System.out.println(c); //与之前相等 Thread.sleep(2000); c = supplier.get(); System.out.println(c); //与之前不等