Java – Collect NoSuchMethodError, stream() method when called

Collect NoSuchMethodError, stream() method when called… here is a solution to the problem.

Collect NoSuchMethodError, stream() method when called

  public int getColsBy(final Collection<Room> rooms) {
    return rooms.stream()
        .max((lhs, rhs) -> lhs.right - rhs.right).get().right;
  }

I

tried to execute this code in my libgdx project, but I only have exceptions!

04-15 22:34:21.136 32610-32636/com.mygdx.game E/AndroidRuntime: FATAL EXCEPTION: GLThread 8116
                                                            Process: com.mygdx.game, PID: 32610
                                                            java.lang.NoSuchMethodError: No interface method stream()Ljava/util/stream/Stream; in class Ljava/util/Collection; or its super classes (declaration of 'java.util.Collection' appears in /system/framework/core-libart.jar)
                                                                at com.mygdx.game.enviroment.LevelParser.getColsBy(LevelParser.java:44)
                                                                at com.mygdx.game.enviroment.LevelParser.parseFrom(LevelParser.java:30)
                                                                at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:38)
                                                                at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:243)
                                                                at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1550)
                                                                at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1272)

Have my build.gradle file

   buildscript {
    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        jcenter()
    }
    dependencies {
        classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6'
        classpath 'com.android.tools.build:gradle:2.1.0-beta1'
        classpath 'org.robovm:robovm-gradle-plugin:1.12.0'
        classpath 'me.tatarka:gradle-retrolambda:3.3.0-beta4'
        classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
        configurations.classpath.exclude group: 'com.android.tools.external.lombok'
    }
}

allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"

version = '1.0'
    ext {
        appName = "my-gdx-game"
        gdxVersion = '1.7.2'
        roboVMVersion = '1.12.0'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.7.0'
    }

repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }
}

project(":desktop") {
    apply plugin: "java"

dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    }
}

project(":android") {
    apply plugin: "android"
    apply plugin: "com.android.application"
    apply plugin: "me.tatarka.retrolambda"

configurations { natives }

dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
    }

}

project(":ios") {
    apply plugin: "java"
    apply plugin: "robovm"

dependencies {
        compile project(":core")
        compile "org.robovm:robovm-rt:$roboVMVersion"
        compile "org.robovm:robovm-cocoatouch:$roboVMVersion"
        compile "com.badlogicgames.gdx:gdx-backend-robovm:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-ios"
    }
}

project(":html") {
    apply plugin: "gwt"
    apply plugin: "war"

dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
        compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
    }
}

project(":core") {
    apply plugin: "java"
    apply plugin: "me.tatarka.retrolambda"

dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
    }
}

tasks.eclipse.doLast {
    delete ".project"
}

What should I do to use flows in my project?

Solution

There are things you can do. Android supports Java 8 starting with version 7 (Nougat).

With (Build.VERSION.SDK_INT >= Build.VERSION_CODES. N) Surround your streaming API calls

public int getColsBy(final Collection<Room> rooms) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES. N) { 
      return rooms.stream().max((lhs, rhs) -> lhs.right - rhs.right).get().right;
}else{
      return "max the old fashion way"
     }
}

I

don’t like external libraries, and that’s what I do.

Related Problems and Solutions