0

I have list of object which in turn has list object in it.

List<User> users = ....;

List<Roles> roles=....;

I want to iterate over user and roles in it and need to compare user.department with role.department list.

User class contains a list of Roles, and a Role contains a list of Departments and And User also contains a Department as String type.

My business logic is different it's something like this. For ease of understanding, I have taken this as an example.

Optional<Role> matchedRole= users
      .stream()
      .flatMap(u -> u.getRoles().stream())
      .filter(r -> r.getDepartmenList().contains(u.getDepartment())
      .findFirst();

Now, how can I access u and the inner stream? Thanks in advance.

4
  • 1
    So your User class contains a list of Roles, and a Role contains a list of Departments? And User also contains a Department? Commented Apr 15, 2021 at 9:40
  • That's correct @MCEmperor, updating the question for others to understand it. Thanks!! Commented Apr 15, 2021 at 10:08
  • Does your example compile? In u.getDepartment() id u parameter accessible? I do not understand exactly what you want to find? Commented Apr 15, 2021 at 10:23
  • @SergeyAfinogenov This doesn't compile, yeah U is not accessible and my question is how to access it. Basically, I want the role which has the same as the user's department. Commented Apr 16, 2021 at 4:34

1 Answer 1

1

Your example is strange, it seems you want to find first role among all user's roles witch contains department the same as in this user. I'm not sure but if you want this:

Optional<Role> matchedRole = users
            .stream().flatMap(u -> u.getRoles()
                                    .stream()
                                    .filter(r -> r.getDepartmentList()
                                                  .contains(u.getDepartment())))
            .findFirst();

If you want to find such one role for each user you can generate Map where, for example, user's name could be a key:

 Map<String, Optional<Role>> matchedUserRole = users
            .stream()
            .collect(Collectors.toMap(User::getName, 
                                      u -> u.getRoles()
                                            .stream()
                                            .filter(r -> r.getDepartmentList()
                                                          .contains(u.getDepartment()))
                                            .findFirst()));
Sign up to request clarification or add additional context in comments.

6 Comments

Basically, I want the role which has same as the user's department
I think this achieves what you wanted to do, no?
Do you want to get only one role or all such roles for every user?@ULLAS K
Only one role for each user. User can have one role matching to his/her department
You should write about this in your question. And in this case my solution is not correct because it search only one Role from all user's roles @ULLAS K.
|

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.