Java函数式编程
August 2021
使用函数式编程风格(lambda表达式、方法引用)对字符串数组进行排序。
一、普通编程风格
1 2 3 4 5 6 7 8 9 10
| List<String> names = Arrays.asList("wyatt2", "wyatt1", "wyatt3"); Collections.sort(names, new Comparator<String>() { @Override public int compare(String a, String b) { return a.compareTo(b); } }); for (String name : names) { System.out.println(name); }
|
二、函数式编程风格
1 2 3 4 5 6 7 8 9 10 11 12 13
| List<String> names = Arrays.asList("wyatt2", "wyatt1", "wyatt3");
Collections.sort(names, (String a, String b) -> { return a.compareTo(b); });
Collections.sort(names, (String a, String b) -> a.compareTo(b));
Collections.sort(names, (a, b) -> a.compareTo(b));
Collections.sort(names, String::compareTo);
names.sort(String::compareTo);
|
发布时间: 2021-08-12 16:42:10
更新时间: 2022-05-11 19:47:57
本文链接: https://wyatt.ink/posts/Code/7766f413.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!