2

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

enter image description here

Vikas
  • 6,868
  • 4
  • 27
  • 41
  • 1
    Can you give us an error message? – Philipp Jul 21 '17 at 11:02
  • You should be able to skip the `map` part. Simply do the cast on `((GroupData)p).getId()` – jensgram Jul 21 '17 at 11:04
  • @jensgram I have edited my question. The problem here is 'id' is of long type and i again need to convert it to String for caomparing. Thats why I was typecasting – Vikas Jul 21 '17 at 11:12
  • @VikasYadav you are missing one additional cast `.map(GroupData::getId).anyMatch(configuredVipUsetGroups::contains)` – Anton Balaniuc Jul 21 '17 at 13:28
  • @AntonBalaniuc I have tried that also, `isValuePresent = allUserGroups.stream() .map(GroupData.class::cast) .map(GroupData::getId) .anyMatch(confUserGroups::contains);` But here getting,'The type GroupData does not define getId(Object) that is applicable here' as getId is not having any argument. – Vikas Jul 21 '17 at 13:41
  • `List` without a type argument is a raw type. Don't use raw types, unless you absolutely have to (because you are dealing with a very old API that doesn't use generics). See: [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Jesper Jul 27 '17 at 07:53
  • @Jesper You are correct but This one is already implemented in our application. So I don't have an option to use generic list. So need to do type cast it. – Vikas Jul 27 '17 at 07:56
  • The type of `p` is `Object`, so each time you need to call a method on it from another type, you need to cast it first. – Jesper Jul 27 '17 at 07:57
  • @Jesper Please check my answer. I know if I explicitly typecast, it will work. Issue is its not working using map method to typecast – Vikas Jul 27 '17 at 08:01

5 Answers5

2

You might try to use something like this:

allUserGroups.stream()
    .map(GroupData.class::cast)
    .map(GroupData::getID)
    .anyMatch(confUserGroups.contains)

for example with String class:

List<Object> list = Arrays.asList("a","ab","abc");
list.stream()
        .map(String.class::cast) // cast to String
        .map(String::getBytes) // call getBytes on every element
        .forEach(System.out::println); 
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
1

Both p's are different variables.

isValuePresent = allUserGroups.stream()
    .map(GroupData.class::cast)
    .anyMatch(p -> confUserGroups.contains(p.getId()));

isValuePresent = allUserGroups.stream()
    .map(GroupData.class::cast)
    .map(GroupData::getId)
    .anyMatch(confUserGroups::contains);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • First part of your answer is throwing same error i. e add cast to p. For second part, getting 'The type GroupData does not define getId(Object) that is applicable here'. getId is not having any argument. – Vikas Jul 21 '17 at 11:50
1

If you already know the type of the values on the list, you could cast the list and then use the stream.

List<GroupData> allUserGroups = (List<GroupData>)UserBC.getAllGroupsForUser(userId, deptID);
confUserGroups= Arrays.asList(configuredSet);

isValuePresent = allUserGroups.stream().anyMatch(p -> confUserGroups.contains(p.getId()));

0

Adding String.valueOf and directly typecasting id solved my issue.

isValuePresent = allUserGroups.stream().anyMatch(p -> configuredVipUserGroups.contains(String.valueOf(((GroupData) p).getId())));

But I still wonder why .map(p -> (GroupData) p) was not able to typecast.

Vikas
  • 6,868
  • 4
  • 27
  • 41
0

Use casting in contains function no need to cast before it.

isValuePresent = allUserGroups.stream()
                            .map(p -> p)
                            .anyMatch(p -> confUserGroups.contains( (GroupData)p.getId()));
azro
  • 53,056
  • 7
  • 34
  • 70
poonam
  • 49
  • 1
  • 4