I am trying to filter a List of Objects in Java in my Android App, for this I followed this answer (Java 8 suggestion) , but as Lambdas aren't supported in Android SDK, I used gradle-retrolambda but I get this runtime-error
java.lang.NoSuchMethodError: No interface method stream()Ljava/util/stream/Stream; in class Ljava/util/List; or its super classes (declaration of 'java.util.List' appears in /system/framework/core-libart.jar)
This is the line of code I am using:
List<CaseDetails> closedCaseDetailsList = caseDetailsList.stream().filter(item -> item.caseClosed.equals(true)).collect(Collectors.toList());
I believe it should work as gradle-retrolambda should be taking care of the Lambdas on Java7.
Next, I tried Lightweight-Stream-API along with gradle-retrolambda and changed my code a little, according to the usage of Lightweight-Stream-API
List<CaseDetails> closedCaseDetailsList = Stream.of(caseDetailsList).filter(item -> item.caseClosed.equals(true)).collect(Collectors.toList());
But it gives me an error over Collectors.toList()
saying
collect
(com.annimon.stream.Collector<? super com.example.yankee.cw.CaseDetails,java.lang.Object,java.lang.Object>)
in Stream cannot be applied
to
(java.util.stream.Collector<T,capture<?>,java.util.List<T>>)
I also tried explicitly type-casting the Stream
to List<CaseDetails>
but that didn't work (of course).
I tried Slack communities, SO Chat-rooms but couldn't find a solution. The closest thing I found to my problem was this question but it is a different error.
Thanks
Unfortunately Retrolambda doesn't backport streams. From the docs:
For a backport of the lightweight streams api you could try:
https://sourceforge.net/projects/streamsupport/
Alternatively you could achieve something similar using RxJava
I can only give you an example for streamsupport
Hope this helps.