public class CodeStyle {
public static void main(String[] args) {
List names1 =new ArrayList();
names1.add("雅虎");
names1.add("阿里巴巴");
names1.add("腾讯");
names1.add("蚂蚁金服");
names1.add("华为");
List names2 =new ArrayList();
names2.add("雅虎");
names2.add("阿里巴巴");
names2.add("腾讯");
names2.add("蚂蚁金服");
names2.add("华为");
CodeStyle codeStyle =new CodeStyle();
System.out.println("使用java7语法:");
codeStyle.sortUsingJava7(names1);
System.out.println(names1);
System.out.println("使用java8语法:");
codeStyle.sortUsingJava7(names2);
System.out.println(names2);
}
/**
* 使用jdk7排序
*
* @param names
*/
private void sortUsingJava7(List names) {
Collections.sort(names, new Comparator() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
}
/**
* 使用jdk8排序
*
* @param names
*/
private void sortUsingJava8(List names) {
Collections.sort(names, (s1, s2) -> s1.compareTo(s2));
}
}