Created
August 14, 2013 06:00
-
-
Save vaskoz/6228390 to your computer and use it in GitHub Desktop.
An example of method references using Java 8 Comparator static methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Comparator; | |
import java.util.List; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.stream.Collectors; | |
public class MethodReferences { | |
public static void main(String[] args) { | |
List<String> list = new ArrayList<String>(Arrays.asList("cat", null, "foo", "aces", "bar", null, "az", null, "bc")); | |
Comparator<String> cmp = Comparator.nullsFirst(Comparator.comparing((String s) -> s.length())) | |
.thenComparing(String.CASE_INSENSITIVE_ORDER); | |
Collections.sort(list, cmp); | |
String result = list.stream().collect(Collectors.toStringJoiner(", ")).toString(); | |
System.out.println(result); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
null, null, null, az, bc, bar, cat, foo, aces |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment