java中lambda表达式的使用案例技巧
日期: 2020-05-31 分类: 跨站数据 322次阅读
1、【Functional Interface】:一个接口仅有一个抽象方法,比如Runnable、Comparator等。在任何一个需要Functional Interface对象的地方,都可以使用lambda表达式。
2、【Method Reference】:将lambda表达式的参数作为参数传递给一个方法,他们的执行效果是相同的,则该lambda表达式
可以使用Method Reference表达,以下两种方式是等价的:
(x) -> System.out.println(x)
System.out::println
Method Reference主要有三种形式:
1.Object::instanceMethod
2.Class::staticMethod
3.Class::instanceMethod
对于前两种方式,对应的lambda表达式的参数和method的参数是一致的,比如:
System.out::println
(x) -> System.out.println(x)
Math::pow
(x, y) -> Math.pow(x, y)
对于第三种方式,对应的lambda表达式的语句体中,第一个参数作为对象,调用method,将其它参数
String::compareToIgnoreCase
(s1, s2) -> s1.compareToIgnoreCase(s2)
Constructor Reference与Method Reference类似,只不过是特殊的method:new,具体调用的是哪个构造函数,由上下文环境决定,比如:
List<String> labels = ...;
Stream<Button> stream = labels.stream().map(Button::new);
Button::new等价于(x) -> Button(x),所以调用的构造函数是:Button(x);
除了创建单个对象,也可以创建对象数组,如下面两种方式等价:
int[]::new
(x) -> new int[x]
====== Map ======
1、map遍历
itemsMap.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v));
====== List ======
1、List排序
1)、list升序
//原始版
words.sort((Word first, Word second) -> first.getName().compareTo(second.getName()));
//精简版
Collections.sort(words, Comparator.comparing(Word::getName));
//精简版,推荐使用这种
words.sort(Comparator.comparing(Word::getName));
//多重排序(连个都升序)
words.sort(Comparator.comparing(Word::getName).thenComparingInt(Word::getCountry));
//多重排序(第一个升,第二个降)
words.sort(Comparator.comparing(Word::getName).reversed().thenComparingInt(Word::getCountry).reversed());
2)、list降序
words.sort(Comparator.comparing(Word::getName).reversed());
2、list遍历
words.forEach(System.out::println);
3、list转Map(key去重)
Map<String, Word> wordMapMap = wordList.stream().collect(Collectors.toMap(Word::getName, k -> k, (k1, k2) -> k1));
list转Map(key不能重复)
Map<Integer,Word> userMap = wordList.stream().collect(Collectors.toMap(Word::getCountry, k -> k));
4、分组
Map<String, List<Word>> groupMap = wordList.stream().collect(Collectors.groupingBy(Word::getName));
多重分组
Map<String, Map<Integer, List<Word>>> map2 = words.stream().collect(Collectors.groupingBy(Word::getName,Collectors.groupingBy(Word::getCountry)));
5、匹配过滤
List<Word> filterList = wordList.stream().filter(v -> v.getName().equals("tiger")).collect(Collectors.toList());
wordList.stream().filter(v -> v.getName().contains("老")).forEach(System.out::println);
List<Word> filterList = wordList.stream().filter(v -> v.getCountry() > 2).collect(Collectors.toList());
Predicate<Word> equals1 = v -> v.getCountry().equals(1);
Predicate<Word> contains1 = v -> v.getName().contains("tiger");
List<Word> filterList = wordList.stream().filter(contains1.and(equals1)).collect(Collectors.toList());
List<Double> filteredCost = cost.stream().filter(x -> x > 25.0).collect(Collectors.toList());
6、list统计求和
int sum = wordList.stream().mapToInt(Word::getCountry).sum();
double sum = words.stream().mapToDouble(Word::getCountry).sum();
7、分组统计个数
Map<String, Long> collectCnt = wordList.stream().collect(Collectors.groupingBy(Word::getName, Collectors.counting()));
8、分组统计
Map<String, LongSummaryStatistics> map3 = words.stream().collect(Collectors.groupingBy(Word::getName,Collectors.summarizingLong(Word::getCountry)));
多重分组统计
Map<String, Map<Integer, LongSummaryStatistics>> map3 = words.stream().collect(Collectors.groupingBy(Word::getName,Collectors.groupingBy(Word::getCountry,Collectors.summarizingLong(Word::getCountry))));
9、去重(需根据实际情况重写Word的equals与hashCode)
List<Word> list1= list.stream().distinct().collect(Collectors.toList());
10、数据拼装
String result = words.stream().map(Word::getName).collect(Collectors.joining("," , "[" , "]"));
12、对象集合转属性集合
List<String> result = words.stream().map(Word::getName).collect(Collectors.toList());
List<String> result = words.stream().map(y -> y.getName().concat(".jpg")).collect(Collectors.toList());
====== 数组 ======
1、数组排序
1)、数组升序->原始版
Arrays.sort(people, (first, second) -> Integer.compare(first.length(), second.length()));
数组升序->精简版
Arrays.sort(people, Comparator.comparingInt(String::hashCode));
2)、数组降序
Arrays.sort(people, (second, first) -> Integer.compare(first.length(), second.length()));
2、数组统计某元素个数
long num = Arrays.stream(name).filter(x -> x.equals("tiger")).count();
3、数组去重过滤并转化成集合
List<String> filterList = Arrays.stream(name).filter(x -> !x.equals("赵强")).collect(Collectors.toList());
4、数组过滤,并对元素加后缀
List<String> filterList = Arrays.stream(name).filter(x -> !x.equals("tiger")).map(y -> y.concat(".jpg")).collect(Collectors.toList());
5、数组统计求和
int num = Arrays.stream(arr).reduce(0, Integer::sum);
double sum = Arrays.asList(10.0, 20.0, 30.0).stream().map(x -> x + x * 0.05).reduce(Double::sum).get();
double sum = Stream.of(10.0, 20.0, 30.0).map(x -> x + x * 0.05).reduce(Double::sum).get();
int sum = Stream.of(1, 2, 3, 4, 5).map(x -> x + x * 5).reduce(Integer::sum).get();
====== 求和 ======
BinaryOperator<Integer> add = Integer::sum;
Integer x = add.apply(20, 30);
====== 线程 ======
private static void lambdaRunnable() {
int age = 20;
Runnable r2 = () -> {
System.out.println(age);
System.out.println("Hello world two!"+age);
};
r2.run();
}
除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog
上一篇: NoSQL 数据库
精华推荐