Java 8 makes your code much more readble and simpler.
In this post, I will show you Comparator example.
In this post, I will show you Comparator example.
Beofre Java 8
I always had to write and put utility comparator for String in my program. Annoying...package com.dukesoftware.utils.common; import java.util.Comparator; public enum StringComparator implements Comparator<String> { Instance; public int compare(String o1, String o2) { if(o1 == nul){ if(o2 == null) return 0; return -1; } return o1.compareTo(o2); } }Of course you can use Java Commons library, but it is too much to add jar to buildpath only for small code snippet.
Java 8 Lambda Expression
See this code! Quite short!Comparator.nullsFirst(Comparator.comparing(String::toString));
コメント