走近Guava(二): 函數式編程

函數式編程:

使用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<City> mainCities = new HashSet<City>();
}
如今你想在一個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,查找某州的城市列表 java

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<T> {
     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> {
       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<City> 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); //與以前不等
Guava函數式編程基礎,後面集合處理中,將體現得更強大。

不吝指正。 app

相關文章
相關標籤/搜索