1

I have two ArrayList userActions and actionsToCheck of type enum Action.

I want to check if all the elements in actionsToCheck is present in userActions.

Is the following good enough or is there a better way?

private boolean actionsAllowed(ArrayList<Action> userActions, ArrayList<Action> actionsToCheck){
    return actionsToCheck.stream().allMatch(action-> actionAllowed(userActions,action));
}

private boolean actionAllowed(ArrayList<Actions> userActions, Action action){
    return userActions.stream().anyMatch(userAction -> userAction == action);
}

ArrayList<Actions> userAction=new ArrayList<>();
userAction.add(ADD_USER);
userAction.add(DELETE_USER);
userAction.add(MODIFY_USER);

ArrayList<Actions> actionsToCheck=new ArrayList<>();
actionsToCheck.add(ADD_USER);
actionsToCheck.add(DELETE_USER);

actionsAllowed(userAction,actionsToCheck) //should return true
2
  • 2
    Whether or not it is good, it is unreadable. I suggest making a predicate function out of this, naming it well, and reducing the "chained calls off the edge of the stream". Commented Jan 19, 2015 at 11:50
  • added another function and sample test. better? Commented Jan 19, 2015 at 12:23

1 Answer 1

2

You could simply use Collection.containsAll(collection) to do your check.

In addition I would recommend using EnumSet instead of ArrayList.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.