Am使用Collectins2.filter
方法来过滤ArrayList of Arraylists
中的值,它给出了错误。它可以与普通ArrayList
完美地进行过滤。
请找到我的ArrayList of Arraylists
所在的POJO类。
Menu.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;
}
};
}
答案 0 :(得分:0)
您不会在方法内部发送相同的泛型,因此无法调用该方法。
一种更清晰的方法是将它们提取到特定变量中。
ArrayList<ArrayList<Menu>> newChildMenus = menu.getNewChildMenus();
Predicate<? super Menu> predicate = byRoleAndPermissions(role, permissionsSet);
您现在可以清楚地看到类型不同。 尽管如此,您的方法仍要等待相同的类型才能工作,因为其签名为
filter(Collection<E> collection, Predicate<? super E> predicate);
如果ArrayList<Menu>
是Menu
的超类,那显然行得通。
答案 1 :(得分:0)
您的Predicate
和他们的Predicate
是不同的类型!
我也花了很长时间ing着这个头。事实证明,有一个非常微妙的陷阱:Collections2.filter()
的参数是我在使用com.google.common.base.Predicate
时的java.util.function.Predicate
。
com.google.common.base.Predicate
的javadoc中实际上提到了一种解决方法:
要在期望使用
java.util.function.Predicate
的地方使用com.google.common.base.Predicate
,请使用方法引用predicate::test
。
所以我想您需要写filter(..., byRoleAndPermissions(role, permissionsSet)::test)
。