2 回答

TA貢獻2011條經驗 獲得超2個贊
Java 8 附帶了lambda 表達式 ( ->)。為了使用 Java 7 中的代碼,您必須將它們替換為匿名類。
如果您使用像 IntelliJ 這樣的 IDE,它可以為您完成這項工作。將光標移動到->,然后點擊ALT + ENTER。應該會出現一個彈出窗口,并且應該有一個選項Replace lambda with anonymous class。
.filter(a -> {
if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {
return true;
} else {
return false;
}
})
到
.filter(new java.util.function.Predicate<SingularAttribute<MyEntity, ?>>() {
@Override
public boolean test(SingularAttribute<MyEntity, ?> a) {
if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {
return true;
} else {
return false;
}
}
})
此外,您還必須從包中刪除您正在使用的所有內容java.util.function。
您可以將其替換.filter()為 for 循環和其中的 if 語句。為此,.map()您必須使用 for 循環修改先前過濾的集合。
new Specification<MyEntity>() {
@Override
public Predicate toPredicate(Root<MyEntity> root, CriteriaQuery<?> cq, CriteriaBuilder builder) {
List<SingularAttribute<MyEntity, ?>> tempAttributes = new ArrayList<>();
for (SingularAttribute<MyEntity, ?> attribute : root.getModel().getDeclaredSingularAttributes()) {
if (attribute.getJavaType().getSimpleName().equalsIgnoreCase("string")) {
tempAttributes.add(attribute);
}
}
final Predicate[] predicates = new Predicate[tempAttributes.size()];
for (int i = 0; i < tempAttributes.size(); i++) {
predicates[i] = builder.like(root.<MyEntity>get(tempAttributes.get(i).getName()), finalText);
}
return builder.or(predicates);
}
};
我自己沒有嘗試過,但這應該有效,或者至少給了你第一步。

TA貢獻1802條經驗 獲得超6個贊
你必須更換
- stream - filter - map
因為 Java 7 中沒有 Streaming API。
通過替換我的意思是你必須遍歷 getDeclaredSingularAttributes() 并過濾元素并映射它。
也Predicate[]::new
必須替換為new Predicate[]
因為現在有方法引用。
正如 Rashin 所說,如果將源代碼級別設置為 Java 7,這可以通過 IDE 完成,它將提供幫助。
添加回答
舉報