比较器 Comparator 接口
lamda 表达式定法 和 比较器类写法
public class StringComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
s1 = StringUtils.isEmpty(s1) ? "0" : s1;
s2 = StringUtils.isEmpty(s2) ? "0" : s2;
return Integer.valueOf(s1) - Integer.valueOf(s2);
}
static Comparator<String> strComparator = (str1, str2) -> {
str1 = StringUtils.isEmpty(str1) ? "0" : str1;
str2 = StringUtils.isEmpty(str2) ? "0" : str2;
return Integer.valueOf(str1) - Integer.valueOf(str2);
};
public static void main(String[] args) {
List<String> strings = Arrays.asList(new String[]{"311", "22", "44"});
strings.sort(strComparator);
for (String string : strings) {
System.out.println(string);
}
}
}
标签:Lamda,String,s2,s1,valueOf,str2,比较,表达式,str1
From: https://www.cnblogs.com/aaacarrot/p/17108317.html