Skip to content Skip to sidebar Skip to footer

Collection Nosuchmethoderror When Caling ,stream() Method

public int getColsBy(final Collection rooms) { return rooms.stream() .max((lhs, rhs) -> lhs.right - rhs.right).get().right; } I try to execute this

Solution 1:

There is something you can do. Android does support Java 8 from version 7 (Nougat).

Surround your streams api call with (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)

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 this is what I do.

Solution 2:

There is nothing you can do. Android is not supporting Java 8 (Jack&Jill is going to support Java 8 partially).

Only lambdas are supported by using third party plugin retrolambda.

Instead of Java 8 you can try RxJavahttps://github.com/ReactiveX/RxJava

Solution 3:

You should Surround your streams api call with Build.VERSION.SDK_INT >= Build.VERSION_CODES.N

I will use an example that takes a string id and multiple placeholder for the string.

// Return string with placeholdersif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

    return context.getResources().getString(stringId, Arrays.stream((newArgs)).toArray());

} else {

    //noinspection ConfusingArgumentToVarargsMethodreturn context.getResources().getString(
                    stringId,
                    Arrays.copyOf(newArgs,
                            newArgs.length,
                            String[].class)
    );
}

My full example function is as below

/**
  * Function to get string resource with multiple integer placeholders
  *
  * @param context  - for getting resources
  * @param stringId - string resource id
  * @param args     - placeholders ids
 */
 @RequiresApi(api = Build.VERSION_CODES.N)
 publicstaticString getStringResource(@NonNull Context context, int stringId, @NonNull Integer... args) {

    // Create new Object array with same size as args lengthObject[] newArgs = newObject[args.length];

    // Loop through passed object with string idfor (int i = 0; i < args.length; i++) {

        // Get string from string id adding them to new Object array
        newArgs[i] = context.getResources().getString(args[i]);
    }

    // Return string with placeholdersif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

        return context.getResources().getString(stringId, Arrays.stream((newArgs)).toArray());

    } else {

        //noinspection ConfusingArgumentToVarargsMethodreturn context.getResources().getString(
                stringId,
                Arrays.copyOf(newArgs,
                        newArgs.length,
                        String[].class
                )
        );
    }
}

Post a Comment for "Collection Nosuchmethoderror When Caling ,stream() Method"