13

I am trying to sort an array of Strings by reverse order (ignoring case), without modifying it, and just printing it. So I am using Java8 stream. But I can't manage to do it.

Here is my attempt :

package experimentations.chapter02;

import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;

public class StringStream {

    public static void main(String[] args) {
        sortStrings();
    }

    public static void sortStrings(){
        String[] stringsArray = "The quick brown fox has a dirty ladder".split("\\s+");
        System.out.println(
                Arrays.stream(stringsArray)
                .sorted(Comparator.comparing(String::toLowerCase).reversed())
                .collect(Collectors.toList())
        );
    }

}

The problem here is that String::toLowerCase is not accepted in the static method Comparator.comparing.

Meanwhile, I managed to sort the array, but modifying it:

public static void sortStrings(){
        String[] stringsArray = "The quick brown fox has a dirty ladder".split("\\s+");
        System.out.println(
                Arrays.stream(stringsArray)
                .map(String::toLowerCase)
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toList())
        );
}

So, what is the simpliest workaround?

2
  • I found the solution : .sorted((String e) -> e.toLowerCase) . Meanwhile, I've asked a deletion of the post, as it seems so obvious to me know, I could have found it without having asked the question ...
    – loloof64
    Commented Apr 8, 2014 at 12:37
  • 2
    Your first code snippet compiles fine. Are you on an old build of Java 8? Or maybe compiling in eclipse? The problem you mention was fixed in build 129. See also stackoverflow.com/q/21751830/829571
    – assylias
    Commented Apr 8, 2014 at 13:11

2 Answers 2

26

The problem is, that Java can not deduce the generic types for some complex expressions. The first statement works, whereas the second statement leads to a compile-time error:

Comparator<String> comparator = Comparator.comparing(String::toLowerCase);
Comparator<String> comparator = Comparator.comparing(String::toLowerCase).reversed();

There are several ways to solve the problem. Here are three of them:

Store the intermediate Comparator in a variable:

Comparator<String> comparator = Comparator.comparing(String::toLowerCase);
System.out.println(
            Arrays.stream(stringsArray)
            .sorted(comparator.reversed())
            .collect(Collectors.toList()));

Use String.CASE_INSENSITIVE_ORDER:

System.out.println(
            Arrays.stream(stringsArray)
            .sorted(String.CASE_INSENSITIVE_ORDER.reversed())
            .collect(Collectors.toList()));

Add explicit type parameters:

System.out.println(
            Arrays.stream(stringsArray)
            .sorted(Comparator.<String,String>comparing(String::toLowerCase).reversed())
            .collect(Collectors.toList()));
9
  • The first code snippet posted by the OP compiles fine.
    – assylias
    Commented Apr 8, 2014 at 13:12
  • @assylias: About which code snippet are you talking? The first code snippet posted by the OP doesn't compile here.
    – nosid
    Commented Apr 8, 2014 at 13:38
  • Arrays.stream(stringsArray).sorted(Comparator.comparing(String::toLowerCase).reversed()).collect(Collectors.toList()) compiles with javac/netbeans on b132.
    – assylias
    Commented Apr 8, 2014 at 13:39
  • 1
    @assylias: Well, it doesn't compile with the Java Compiler, that is part of the latest release of JDK8 by Oracle.
    – nosid
    Commented Apr 8, 2014 at 13:58
  • 1
    @assylias: I am not sure, why there is a compilation error at all. The rules regarding method references are quite complex. Therefore, I have created a new question: stackoverflow.com/questions/22945870/…
    – nosid
    Commented Apr 8, 2014 at 18:58
7

I found the solution :

 .sorted((String e) -> e.toLowerCase) 

I think the problem with the syntax

 .sorted(String::toLowerCase)

is that the compiler then expects to pass an Object to the instance method toLowerCase of String. So I need to make my own lambda method, without ignoring the type of the lambda parameter (String), otherwise the compiler still can't resolve it.

2
  • IntelliJ tells me that what you tried at first is a cyclic inference, which is forbidden. Now, I try to understand what is a cyclic inference :) Commented Apr 8, 2014 at 13:05
  • As telled me assylias, it seems that some IDE has not yet real support for Java8 features. Also, I tried a similar code inside Netbeans, without any problem.
    – loloof64
    Commented Apr 8, 2014 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.