数据库逐步升级
知识点:
java8匿名函数的使用
LinkHashMap的使用,按顺序添加;
TreeMap的使用,自动排序,可使用subMap(from, true, to, true);
public class DataBaseUpgradeFunction {
private static final int MONTH_5 = 5;
private static final int MONTH_6 = 6;
private static final int MONTH_7 = 7;
public static void main(String[] args) {
int oldDate = 4;
int newDate = 7;
// 方法一:使用LinkHashMap
Map<Integer, Function<Integer, Void>> linkDates = throughMonth_LinkHashMap();
for (Integer key : linkDates.keySet()) {
if (key < oldDate) {
continue;
}
System.out.println("i: " + key);
linkDates.get(key).apply(newDate);
}
// 方法二:使用TreeMap
TreeMap<Integer, Function<Integer, Void>> treeDates = throughMonth_TreeMap();
for (Integer key : treeDates.subMap(oldDate, false, newDate, true).keySet()) {
treeDates.get(key).apply(newDate);
}
}
// 使用LinkHashMap排序,需要人工自觉遵守添加顺序,改进方法,使用TreeMap
private static Map<Integer, Function<Integer, Void>> throughMonth_LinkHashMap() {
Map<Integer, Function<Integer, Void>> dates = new LinkedHashMap<>(); // 使用排序的哈希算法
dates.put(MONTH_5, throughMonth5);
dates.put(MONTH_6, throughMonth6);
dates.put(MONTH_7, throughMonth7);
return dates;
}
// 使用TreeMap
private static TreeMap<Integer, Function<Integer, Void>> throughMonth_TreeMap() {
TreeMap<Integer, Function<Integer, Void>> dates = new TreeMap<>();
dates.put(MONTH_7, throughMonth7);
dates.put(MONTH_5, throughMonth5);
dates.put(MONTH_6, throughMonth6);
return dates;
}
private static final Function<Integer, Void> throughMonth5 = (Integer date) -> {
System.out.println("走过了5月份:" + date);
return null;
};
private static final Function<Integer, Void> throughMonth6 = (Integer date) -> {
System.out.println("走过了6月份");
return null;
};
private static final Function<Integer, Void> throughMonth7 = (Integer date) -> {
System.out.println("走过了7月份");
return null;
};
}