0

I have a List like

@OneToMany
private List<Personal> personal;

Later on I want to filter results

List<Integer> ist = personal.stream().map(p -> p.getPnr()).collect(Collectors.toList());

But I always get an empty list. When I create an old school for-loop, things work as expected. I added fetch=FETCH_TYPE.EAGER to the @OneToMany annotation, but it didn't fix the problem.

The debugger says, at runtime, 'personal' is a 'indirect list'. Is this the problem? And what can I do to fix it?


Code that work fine

List<Integer> ist = new ArrayList<>(); 
for (Personal p : personal) 
   ist.add(p.getPnr());
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
user1119859
  • 669
  • 2
  • 9
  • 20
  • 2
    Can you share your *old school for-loop* – Youcef LAIDANI Nov 26 '19 at 12:47
  • 1
    Can you try copying the contents of the `personal` list to another new list e.g: `List list = new ArrayList(personal)` and then attempt to stream? – akortex Nov 26 '19 at 12:50
  • Are you using excel – Youcef LAIDANI Nov 26 '19 at 12:50
  • List ist = new ArrayList<>(); for (Personal p : personal) ist.add(p.getPnr()); – user1119859 Nov 26 '19 at 12:51
  • 1
    Also I assume you're using EclipseLink? – akortex Nov 26 '19 at 12:52
  • 1
    What is the class of the list object assigned to `personal`? The problem could be that that class doesn't implement `stream()` properly. – Stephen C Nov 26 '19 at 12:52
  • 1
    At least it works this way: List ist = new ArrayList<>(personal).stream().map(p -> p.getPnr()).collect(Collectors.toList()); – user1119859 Nov 26 '19 at 12:54
  • yes, I'm using EclipseLink – user1119859 Nov 26 '19 at 12:56
  • Will [this](https://stackoverflow.com/questions/35362581/stream-api-not-working-for-lazy-loaded-collections-in-eclipselink-glassfish) help maybe? – Michał Krzywański Nov 26 '19 at 13:00
  • See [stream on JPA lazy list](https://stackoverflow.com/q/37925649/2711488) – Holger Nov 26 '19 at 13:20
  • @JoopEggen since streams do not allow to modify the source collection, `Collections.unmodifiableList(list).stream()` will simply delegate to `list.stream()` – Holger Nov 26 '19 at 13:21
  • @Holger the link of **michalk** poinst to some bugs in eclipseLink with as workaround wrapping the list in an `unmodifiableList`. Which is better than a `new ArrayList(list)` – Joop Eggen Nov 26 '19 at 13:24
  • @JoopEggen his workaround is `Collections.unmodifiableList(new ArrayList<>(domains))`, which works due to the copying operation. The `unmodifiableList` does not contribute to the work-around but only makes the list unmodifiable, which was already present in the question's code. When you omit the copying into an `ArrayList`, you omitted the actual workaround. – Holger Nov 26 '19 at 13:28
  • @Holger I am getting dumb. Thanks. – Joop Eggen Nov 26 '19 at 13:30

1 Answers1

1

I'm not sure about what the actuall problem is, but creating a new List works for me:

List<Integer> ist = new ArrayList<>(personal).stream().map(p -> p.getPnr()).collect(Collectors.toList());
user1119859
  • 669
  • 2
  • 9
  • 20
  • One of the comments should have pointed you to EclipseLink bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=446236#c12 which is fixed in the 2.7 stream. – Chris Nov 27 '19 at 19:20