0

I am using

java version 1.8._101

Android studio 2.1.3

Gradle version 2.14.1

minSdkVersion 15

targetSdkVersion 23

buildToolsVersion '23.0.3'

Try to filter arrayList using java 8 lambda expression like

ArrayList<Object> listObject = ......();
listObject.stream();

but it gives me cannot resolve method stream()

I already refer this

Community
  • 1
  • 1
Pitty
  • 1,907
  • 5
  • 16
  • 34
  • Possible duplicate of [Java 8 Stream API in Android N](http://stackoverflow.com/questions/36112086/java-8-stream-api-in-android-n) – GSerg Aug 20 '16 at 07:49
  • No not working with android N my buildToolsVersion '23.0.3' – Pitty Aug 20 '16 at 07:55

1 Answers1

2

2018 Update: jack is no longer supported. Current Java 8 support howto is available here: https://developer.android.com/studio/write/java8-support.html - Especially useful is the table that shows which parts of Java8 are available at which API Levels.


INFO BELOW IS DEPRECATED

See

Key parts of build.gradle:

android {
  compileSdkVersion 24
  buildToolsVersion "24.0.1"
  ...
  defaultConfig {
    minSdkVersion 24
    targetSdkVersion 24
    ...
    jackOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }

}

When using Stream apis, got an error "Call requires API Level 24". You can hide that behind @TargetApi(24), but then will need an alternative implementation for lower API levels. To use without restrictions, minSdkVersion has to be to 24

After updating build.gradle, make sure in the Project view under external libraries, "Android API 24 Platform" shows up. If not, do Tools->Android->Sync Project with Gradle Files

Was then able to get the following test to pass

@Test
public void testStream() throws Exception {
    assertThat(Arrays.asList(1, 3, 4).stream().reduce(0, (i, j) -> i + j), is(8));
}
Stan Kurdziel
  • 5,476
  • 1
  • 43
  • 42
  • No I tested with new project and list.stream() is there in that project. – Pitty Aug 20 '16 at 09:09
  • NOTE jack is no longer supported. Current Java 8 support howto is available here: https://developer.android.com/studio/write/java8-support.html - Especially useful is the table that shows which parts of Java8 are available at which API Levels. – Stan Kurdziel Jan 29 '18 at 19:13