Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Created August 14, 2013 06:00
Show Gist options
  • Save vaskoz/6228390 to your computer and use it in GitHub Desktop.
Save vaskoz/6228390 to your computer and use it in GitHub Desktop.
An example of method references using Java 8 Comparator static methods
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);
}
}
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