0

This is extension to my this question

I want to fetch id from a List below,

List allUserGroups = UserBC.getAllGroupsForUser(userId, deptID);

This List has Objects which I need to typecast to GroupData entity. GroupData extends PartyData which has getId() method defined in it. So I tried using labda as ,

allUserGroups.stream()
.map(GroupData.class::cast)
.map(GroupData::getId)

Issue here is, compiler shows The type GroupData does not define getId(Object) that is applicable here.

The type GroupData does not define getId(Object) that is applicable here

It works fine with normal for loop,

for(Object userGroup: allUserGroups){
    long usergrp = ((GroupData) userGroup ).getId();
}

Can anyone suggest how to achieve this using Labmda

Vikas
  • 6,868
  • 4
  • 27
  • 41
  • 2
    Why are you using raw List as the type of `allUserGroups`? – Eran Jul 27 '17 at 07:52
  • This one is already implemented in our application. So I don't have an option to use generic list. – Vikas Jul 27 '17 at 07:53
  • The names in the error message do not match the code you posted. Why does the error message mention `LOBEntityDetails` and `getCategoryId` while in your code there is no such class and method? – Jesper Jul 27 '17 at 07:55
  • @Jesper My bad ..edited the question. I was getting it for some other method also. – Vikas Jul 27 '17 at 07:58
  • Is `GroupData.getId()` a `static` method? – Jesper Jul 27 '17 at 08:02
  • No, its just a getter – Vikas Jul 27 '17 at 08:03
  • 3
    This error happens because of the use of raw types. Cast your list to a generic type: `List allUserGroups = (List) UserBC.getAllGroupsForUser(userId, deptID);` then you also don't need the first `map` anymore. – Jesper Jul 27 '17 at 08:05
  • This seems better solution. Accepted. Thank you. – Vikas Jul 27 '17 at 08:45
  • 1
    The unsafe cast shouldn't be necessary. What's the return type of `UserBC.getAllGroupsForUser()`? – shmosel Jul 27 '17 at 09:05
  • @shmosel return type is List `public static List getAllGroupsForUser(long userId, long departmentId) throws PlatformException {\\ code}` – Vikas Jul 27 '17 at 09:10
  • 2
    Then the cast isn't necessary altogether; you can assign the result as is. If you want to avoid the compiler warnings, you can assign the result to a `List>` and your original code should work. This is all assuming you can't modify the method signature, which shouldn't be using a raw type in the first place. – shmosel Jul 27 '17 at 09:16

1 Answers1

0

Used typecast while fetching the list as commented by Jespher and it solved my issue,

List<GroupData> allUserGroups = (List<GroupData>) UserBC.getAllGroupsForUser(userId, deptID);
allUserGroups.stream().map(GroupData::getId)
Vikas
  • 6,868
  • 4
  • 27
  • 41