8

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

2 Answers 2

7

I can only give you an example for streamsupport

import java.util.Arrays;
import java.util.List;

import java8.util.stream.Collectors;
import java8.util.stream.StreamSupport;
import static java.lang.Boolean.TRUE;
import static java.lang.Boolean.FALSE;

public class CaseDetails {

    Boolean caseClosed = FALSE;

    public CaseDetails(Boolean caseClosed) {
        this.caseClosed = caseClosed;
    }

    public String toString() {
        return "caseClosed: " + caseClosed;
    }

    public static void main(String[] args) {
        List<CaseDetails> caseDetailsList = Arrays.asList(
                new CaseDetails(TRUE), new CaseDetails(FALSE), new CaseDetails(
                        TRUE));
        List<CaseDetails> closedCaseDetailsList = StreamSupport
                .stream(caseDetailsList)
                .filter(item -> item.caseClosed.equals(TRUE))
                .collect(Collectors.toList());

        System.out.println(closedCaseDetailsList);
    }
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

Got it. My Collectors was a Java7 class that was imported earlier and it was continuing to use the same. Thanks a lot to you and @Jahnold
7

Unfortunately Retrolambda doesn't backport streams. From the docs:

Retrolambda lets you run Java 8 code with lambda expressions, method references and try-with-resources statements on Java 7, 6 or 5

For a backport of the lightweight streams api you could try:

https://sourceforge.net/projects/streamsupport/

Alternatively you could achieve something similar using RxJava

11 Comments

Thanks, I also tried using Apache Commons with Predicates but faced some errors in that too. I'll give this a try, thanks for the help.
Also, isn't the Lightweight-Stream-API able to make up for this shortcoming on the part of retrolambda ? How can I get around the second error that I have mentioned ?
I've not tried Lightweight-Steam-API only the streamsupport library. It did work but I often found issues like you have above due to the changed interface names - I image this will be where the problem is. I just use Rx now
@Yankee It's not RefStreams.of (that's the equivalent to Java 8 Stream.of and produces a stream that contains your List as the single element). Instead use the static method java8.util.stream.StreamSupport.stream(Collection) and all should be fine.
@Yankee That doesn't make sense. What is com.util.stream? Why do you have a collect method that expects a java.util.stream.Collector instead of a java8.util.stream.Collector. Wrong imports? See my example for code that compiles.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.