I am comparing two lists.
List allUserGroups = UserBC.getAllGroupsForUser(userId, deptID);
List<String> confUserGroups= Arrays.asList(configuredSet);
List one returns Object which I need to typecast to GroupData entity. GroupData has multiple fields and want to compare for one of the fields 'id'. So I used map function to typecast as below,
isValuePresent = allUserGroups.stream().map(p -> (GroupData) p).anyMatch(p -> confUserGroups.contains(p.getId()));
Issue is, for p.getId()
its again asking for typecast. Compiler asks to add cast again. Can anyone please suggest if I missed anything.
EDIT1:
id is of long type otherwise I could have used ((GroupData)p).getId()
EDIT2: Modified the code as answered by Joop, but getting same error
You might try to use something like this:
for example with
String
class:Adding String.valueOf and directly typecasting id solved my issue.
But I still wonder why
.map(p -> (GroupData) p)
was not able to typecast.Both p's are different variables.
Use casting in contains function no need to cast before it.
If you already know the type of the values on the list, you could cast the list and then use the stream.