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 完成,它将提供帮助。
添加回答
举报