Java Lambda Iteration List from JPA -
this question has answer here:
- why stream return no element? 1 answer
i found strange behaviour when using lambda jpa, seems java 8 lambda don't iterate when list object.
for example:
list<myobject> list = anotherobject.getmyobjectlist(); // list list.foreach(myobject -> system.out.println("not printed")); system.out.println("size?: " + list.size()); // print size = 2
i try list.stream().foreach() same results..
after hours of testing found trick
list<myobject> copylist = new arraylist<>(list); // copy list copylist.foreach(myobject -> system.out.println("omg printed!"));
huh? ideas?, bug? or im doing wrong? entity clases working good, relations good... :)
thanks in advance :).
it know concrete class of list returned anotherobject.getmyobjectlist()
. may have bug in iterator.
when copy arraylist using new arraylist<>(list)
, code constructor copies elements source list using toarray
, copies new array that's contained within arraylist.
when call list.foreach
, unless it's been overridden concrete class, ends calling default method foreach
of iterable
, implementation is:
default void foreach(consumer<? super t> action) { objects.requirenonnull(action); (t t : this) { action.accept(t); } }
this standard enhanced-for loop here, nothing magic. loop implemented in terms of list's iterator. try writing out for-loop instead of calling list.foreach(lambda)
, or try calling list.iterator()
, see how many elements out of it. if toarray
works iteration techniques don't, seems bug in implementation of jpa list class's iterator.
Comments
Post a Comment