我使用Collectins2.filter
方法从ArrayList of Arraylists
中过滤值,它给出了错误。它与正常过滤ArrayList
完美配合。
请找到我的POJO课程,在那里有ArrayList of Arraylists
。
菜单.java
public class Menu {
private String name;
private String code;
private String action;
private String css;
private String symbol;
private String feature;
private boolean visibleToExternal = true;
private Set<String> permissions;
private ArrayList<Menu> childMenus;
private ArrayList<ArrayList<Menu>> newChildMenus=new ArrayList<ArrayList<Menu>>();
public boolean hasChildMenus() {
newChildMenus.add(subChildMenus);
return newChildMenus !=null && !newChildMenus.isEmpty();
}
}
请找到我的Predicate
方法实现。
private Predicate<? super Menu> byRoleAndPermissions(final Role role, final Set<String> permissionsSet) {
return new Predicate<Menu>() {
@Override
public boolean apply(Menu menu) {
final boolean filterForExternalUser = !role.isRoleInternal() && !menu.isVisibleToExternal() && !(role.getCode().equals("DLR_ADMN") && menu.getCode().equals("MDFY_USER_PRVG"));
// for dealer and dealer read only related changes : MDFY_USER_PRVG
if(!role.isRoleInternal() && (role.getCode().equals("DLR") || role.getCode().equals("DLR_RD_ONLY")) && menu.getCode().equals("MDFY_USER_PRVG")){
return true;
}
if (filterForExternalUser) {
return false;
}
SetView<String> intersection = Sets.intersection(menu.getPermissions(), permissionsSet);
if (intersection.size() == 0) {
return false;
}
if (menu.hasChildMenus()) {
menu.setChildMenus(new ArrayList<Menu>(filter(menu.getNewChildMenus(), byRoleAndPermissions(role, permissionsSet))));/// giving error - The method filter(Collection<E>, Predicate<? super E>) in the type Collections2 is not applicable for the arguments (ArrayList<ArrayList<Menu>>, Predicate<capture#4-of ? super Menu>)
}
return true;
}
};
}
在filter()
方法实现过程中给出以下错误。
The method filter(Collection<E>, Predicate<? super E>) in the type Collections2 is not applicable for the arguments (ArrayList<ArrayList<Menu>>, Predicate<capture#4-of ? super Menu>)
更新 1
请找到我被修改的代码。 但仍然收到一些错误Return type for the method is missing
private Predicate<? super ArrayList<Menu>> byRoleAndPermissions(final Role role, final Set<String> permissionsSet) {
return new Predicate<ArrayList<Menu>>() {
@Override
public boolean apply(ArrayList<Menu> menu) {
Predicate<? super ArrayList<Menu>> predicate = byRoleAndPermissions(role, permissionsSet);
final boolean filterForExternalUser = !role.isRoleInternal() && !menu.get(0).isVisibleToExternal() && !(role.getCode().equals("DLR_ADMN") && menu.get(0).getCode().equals("MDFY_USER_PRVG"));
// for dealer and dealer read only related changes : MDFY_USER_PRVG
if(!role.isRoleInternal() && (role.getCode().equals("DLR") || role.getCode().equals("DLR_RD_ONLY")) && menu.get(0).getCode().equals("MDFY_USER_PRVG")){
return true;
}
if (filterForExternalUser) {
return false;
}
SetView<String> intersection = Sets.intersection(menu.get(0).getPermissions(), permissionsSet);
if (intersection.size() == 0) {
return false;
}
if (menu.hasChildMenus()) {
menu.setChildMenus(new ArrayList<Menu>(filter(Collection<E> collection, Predicate<? super E> predicate))); // errors coming here
}
return true;
}
};
}
您不会在方法内部发送相同的泛型,因此无法调用该方法。
使其更清晰的一种方法是将它们提取到特定变量中。
ArrayList<ArrayList<Menu>> newChildMenus = menu.getNewChildMenus();
Predicate<? super Menu> predicate = byRoleAndPermissions(role, permissionsSet);
您现在可以清楚地看到类型不同。尽管如此,您的方法仍然等待相同的类型才能工作,因为它的签名是
filter(Collection<E> collection, Predicate<? super E> predicate);
如果ArrayList<Menu>
是超级类Menu
,那会起作用,但事实显然并非如此。
您的Predicate
和他们的Predicate
是不同的类型!
我也花了很长时间挠头。 事实证明,有一个非常微妙的问题:Collections2.filter()
的论点在我使用java.util.function.Predicate
时com.google.common.base.Predicate
。
实际上,com.google.common.base.Predicate
的 javadoc 中提到了一种解决方法:
若要在预期
com.google.common.base.Predicate
中使用java.util.function.Predicate
,请使用方法引用predicate::test
。
所以我想你需要写filter(..., byRoleAndPermissions(role, permissionsSet)::test)
.