Метод Collections2.filter не удовлетворяет переданным аргументам - PullRequest
1 голос
/ 09 июля 2019

Я использую метод Collectins2.filter для фильтрации значений из ArrayList of Arraylists и выдает ошибку.Он отлично работает с обычным ArrayList для фильтрации.

Пожалуйста, найдите мой класс POJO, где у меня ArrayList of Arraylists.

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;
          }
        };
      }

1 Ответ

0 голосов
/ 09 июля 2019

Вы не отправляете тот же общий тип внутри метода, поэтому метод не может быть вызван.

Чтобы прояснить ситуацию, нужно извлечь их из определенных переменных.

ArrayList<ArrayList<Menu>> newChildMenus = menu.getNewChildMenus();
Predicate<? super Menu> predicate = byRoleAndPermissions(role, permissionsSet);

Теперь вы можете ясно видеть, что тип не совпадает. Тем не менее, ваш метод ожидает того же типа, чтобы иметь возможность работать, так как его подпись

filter(Collection<E> collection, Predicate<? super E> predicate);

Это сработало бы, если бы ArrayList<Menu> был суперклассом Menu, что явно не так.

...