Java lambda表达式使用方式

基础用法:

  1. Stream的filter与谓语逻辑:https://www.kancloud.cn/hanxt/javacrazy/1575714
  2. Stream管道流map的基础用法:https://www.kancloud.cn/hanxt/javacrazy/1577825
  3. Stream的状态与并行操作:https://www.kancloud.cn/hanxt/javacrazy/1589649
  4. 像使用SQL一样排序集合:https://www.kancloud.cn/hanxt/javacrazy/1605244
  5. 函数式接口Comparator:https://www.kancloud.cn/hanxt/javacrazy/1605260
  6. Stream查找与匹配元素:https://www.kancloud.cn/hanxt/javacrazy/1630359
  7. Stream集合元素归约:https://www.kancloud.cn/hanxt/javacrazy/1641322
  8. StreamAPI终端操作:https://www.kancloud.cn/hanxt/javacrazy/1641151
  9. java8如何排序Map:https://www.kancloud.cn/hanxt/javacrazy/1641153
  10. Stream流逐行文件处理:https://www.kancloud.cn/hanxt/javacrazy/1825288
  11. java8-forEach:https://www.kancloud.cn/hanxt/javacrazy/1579397

进阶用法:

  1. 根据条件去重:
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
    Map<Object, Boolean> seen = new ConcurrentHashMap<>();
    return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null;
}
List<User> users = new LinkedList<>();
users.add(new User("Jim"));
users.add(new User("Jim"));
users.add(new User("Tom"));
users.add(new User("Leo"));
 
List<User> distinctUsers = users.stream().filter(distinctByKey(User::getName)).collect(Collectors.toList());
System.out.println(distinctUsers);//[Jim, Tom, Leo]

2、联合排序:lambda表达式对LIST对象多个字段进行排序_心语豫园的博客-CSDN博客_lamda表达式根据两个字段排序

3、将String类型的classIds转成List<Integer>类型

List<Integer> collect = Arrays.stream(classIds.split(",")).mapToInt(Integer::parseInt).boxed().collect(Collectors.toList());

 

posted @ 2021-09-18 10:02  liaozk  阅读(91)  评论(0编辑  收藏  举报