• 10.常用的lambda表达式


    1. list转map

    工作中,我们经常遇到listmap的案例。Collectors.toMap就可以把一个list数组转成一个Map。代码如下:

     1 public class TestLambda {
     2 
     3     public static void main(String[] args) {
     4 
     5         List<UserInfo> userInfoList = new ArrayList<>();
     6         userInfoList.add(new UserInfo(1L, "捡田螺的小男孩", 18));
     7         userInfoList.add(new UserInfo(2L, "程序员田螺", 27));
     8         userInfoList.add(new UserInfo(2L, "捡瓶子的小男孩", 26));
     9 
    10         /**
    11          *  list 转 map
    12          *  使用Collectors.toMap的时候,如果有可以重复会报错,所以需要加(k1, k2) -> k1
    13          *  (k1, k2) -> k1 表示,如果有重复的key,则保留第一个,舍弃第二个
    14          */
    15         Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getUserId, userInfo -> userInfo, (k1, k2) -> k1));
    16         userInfoMap.values().forEach(a->System.out.println(a.getUserName()));
    17     }
    18 }
    19 
    20 //运行结果
    21 捡田螺的小男孩
    22 程序员田螺

    类似的,还有Collectors.toList()Collectors.toSet(),表示把对应的流转化为list或者Set

    2. filter()过滤

    从数组集合中,过滤掉不符合条件的元素,留下符合条件的元素。

     1 List<UserInfo> userInfoList = new ArrayList<>();
     2 userInfoList.add(new UserInfo(1L, "捡田螺的小男孩", 18));
     3 userInfoList.add(new UserInfo(2L, "程序员田螺", 27));
     4 userInfoList.add(new UserInfo(3L, "捡瓶子的小男孩", 26));
     5         
     6 /**
     7  * filter 过滤,留下超过18岁的用户
     8  */
     9 List<UserInfo> userInfoResultList = userInfoList.stream().filter(user -> user.getAge() > 18).collect(Collectors.toList());
    10 userInfoResultList.forEach(a -> System.out.println(a.getUserName()));
    11         
    12 //运行结果
    13 程序员田螺
    14 捡瓶子的小男孩

    3. foreach遍历

    foreach 遍历list,遍历map,真的很丝滑。

     1 /**
     2  * forEach 遍历集合List列表
     3  */
     4 List<String> userNameList = Arrays.asList("捡田螺的小男孩", "程序员田螺", "捡瓶子的小男孩");
     5 userNameList.forEach(System.out::println);
     6  
     7 HashMap<String, String> hashMap = new HashMap<>();
     8 hashMap.put("公众号", "捡田螺的小男孩");
     9 hashMap.put("职业", "程序员田螺");
    10 hashMap.put("昵称", "捡瓶子的小男孩");
    11 /**
    12  *  forEach 遍历集合Map
    13  */
    14 hashMap.forEach((k, v) -> System.out.println(k + ":\t" + v));
    15 
    16 //运行结果
    17 捡田螺的小男孩
    18 程序员田螺
    19 捡瓶子的小男孩
    20 职业: 程序员田螺
    21 公众号: 捡田螺的小男孩
    22 昵称: 捡瓶子的小男孩

    4. groupingBy分组

    提到分组,相信大家都会想起SQLgroup by。我们经常需要一个List做分组操作。比如,按城市分组用户。在Java8之前,是这么实现的:

     1 List<UserInfo> originUserInfoList = new ArrayList<>();
     2 originUserInfoList.add(new UserInfo(1L, "捡田螺的小男孩", 18,"深圳"));
     3 
     4 originUserInfoList.add(new UserInfo(3L, "捡瓶子的小男孩", 26,"湛江"));
     5 originUserInfoList.add(new UserInfo(2L, "程序员田螺", 27,"深圳"));
     6 Map<String, List<UserInfo>> result = new HashMap<>();
     7 for (UserInfo userInfo : originUserInfoList) {
     8   String city = userInfo.getCity();
     9   List<UserInfo> userInfos = result.get(city);
    10   if (userInfos == null) {
    11       userInfos = new ArrayList<>();
    12       result.put(city, userInfos);
    13     }
    14   userInfos.add(userInfo);
    15 }

    而使用Java8的groupingBy分组器,清爽无比:

    1 Map<String, List<UserInfo>> result = originUserInfoList.stream()
    2 .collect(Collectors.groupingBy(UserInfo::getCity));

    5. sorted+Comparator 排序

    工作中,排序的需求比较多,使用sorted+Comparator排序,真的很香。

     1 List<UserInfo> userInfoList = new ArrayList<>();
     2 userInfoList.add(new UserInfo(1L, "捡田螺的小男孩", 18));
     3 userInfoList.add(new UserInfo(3L, "捡瓶子的小男孩", 26));
     4 userInfoList.add(new UserInfo(2L, "程序员田螺", 27));
     5 
     6 /**
     7  *  sorted + Comparator.comparing 排序列表,
     8  */
     9 userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge)).collect(Collectors.toList());
    10 userInfoList.forEach(a -> System.out.println(a.toString()));
    11 
    12 System.out.println("开始降序排序");
    13 
    14 /**
    15  * 如果想降序排序,则可以使用加reversed()
    16  */
    17 userInfoList = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge).reversed()).collect(Collectors.toList());
    18 userInfoList.forEach(a -> System.out.println(a.toString()));
    19 
    20 //运行结果
    21 UserInfo{userId=1, userName='捡田螺的小男孩', age=18}
    22 UserInfo{userId=3, userName='捡瓶子的小男孩', age=26}
    23 UserInfo{userId=2, userName='程序员田螺', age=27}
    24 开始降序排序
    25 UserInfo{userId=2, userName='程序员田螺', age=27}
    26 UserInfo{userId=3, userName='捡瓶子的小男孩', age=26}
    27 UserInfo{userId=1, userName='捡田螺的小男孩', age=18}

    6.distinct去重

    distinct可以去除重复的元素:

    1 List<String> list = Arrays.asList("A", "B", "F", "A", "C");
    2 List<String> temp = list.stream().distinct().collect(Collectors.toList());
    3 temp.forEach(System.out::println);

    7. findFirst 返回第一个

    findFirst 很多业务场景,我们只需要返回集合的第一个元素即可:

    1 List<String> list = Arrays.asList("A", "B", "F", "A", "C");
    2 list.stream().findFirst().ifPresent(System.out::println);

    8. anyMatch是否至少匹配一个元素

    anyMatch 检查流是否包含至少一个满足给定谓词的元素。

    1 Stream<String> stream = Stream.of("A", "B", "C", "D");
    2 boolean match = stream.anyMatch(s -> s.contains("C"));
    3 System.out.println(match);
    4 //输出
    5 true

    10. map转换

    map方法可以帮我们做元素转换,比如一个元素所有字母转化为大写,又或者把获取一个元素对象的某个属性,demo如下:

    1 List<String> list = Arrays.asList("jay", "tianluo");
    2 //转化为大写
    3 List<String> upperCaselist = list.stream().map(String::toUpperCase).collect(Collectors.toList());
    4 upperCaselist.forEach(System.out::println);

    11. Reduce

    Reduce可以合并流的元素,并生成一个值

    1 int sum = Stream.of(1, 2, 3, 4).reduce(0, (a, b) -> a + b);
    2 System.out.println(sum);

    12. peek 打印个日志

    peek()方法是一个中间Stream操作,有时候我们可以使用peek来打印日志。

    1 List<String> result = Stream.of("程序员田螺", "捡田螺的小男孩", "捡瓶子的小男孩")
    2             .filter(a -> a.contains("田螺"))
    3             .peek(a -> System.out.println("关注公众号:" + a)).collect(Collectors.toList());
    4 System.out.println(result);
    5 //运行结果
    6 关注公众号:程序员田螺
    7 关注公众号:捡田螺的小男孩
    8 [程序员田螺, 捡田螺的小男孩]

    13. Max,Min最大最小

    使用lambda流求最大,最小值,非常方便。

     1 List<UserInfo> userInfoList = new ArrayList<>();
     2 userInfoList.add(new UserInfo(1L, "捡田螺的小男孩", 18));
     3 userInfoList.add(new UserInfo(3L, "捡瓶子的小男孩", 26));
     4 userInfoList.add(new UserInfo(2L, "程序员田螺", 27));
     5 
     6 Optional<UserInfo> maxAgeUserInfoOpt = userInfoList.stream().max(Comparator.comparing(UserInfo::getAge));
     7 maxAgeUserInfoOpt.ifPresent(userInfo -> System.out.println("max age user:" + userInfo));
     8 
     9 Optional<UserInfo> minAgeUserInfoOpt = userInfoList.stream().min(Comparator.comparing(UserInfo::getAge));
    10 minAgeUserInfoOpt.ifPresent(userInfo -> System.out.println("min age user:" + userInfo));
    11 
    12 //运行结果
    13 max age user:UserInfo{userId=2, userName='程序员田螺', age=27}
    14 min age user:UserInfo{userId=1, userName='捡田螺的小男孩', age=18}

    14. count统计

    一般count()表示获取流数据元素总数。

    1 List<UserInfo> userInfoList = new ArrayList<>();
    2 userInfoList.add(new UserInfo(1L, "捡田螺的小男孩", 18));
    3 userInfoList.add(new UserInfo(3L, "捡瓶子的小男孩", 26));
    4 userInfoList.add(new UserInfo(2L, "程序员田螺", 27));
    5 
    6 long count = userInfoList.stream().filter(user -> user.getAge() > 18).count();
    7 System.out.println("大于18岁的用户:" + count);
    8 //输出
    9 大于18岁的用户:2

    15. 常用函数式接口

    其实lambda离不开函数式接口,我们来看下JDK8常用的几个函数式接口:

    • Function<T, R>(转换型): 接受一个输入参数,返回一个结果
    • Consumer<T> (消费型): 接收一个输入参数,并且无返回操作
    • Predicate<T> (判断型): 接收一个输入参数,并且返回布尔值结果
    • Supplier<T> (供给型): 无参数,返回结果

    Function<T, R> 是一个功能转换型的接口,可以把将一种类型的数据转化为另外一种类型的数据

    1     private void testFunction() {
    2         //获取每个字符串的长度,并且返回
    3         Function<String, Integer> function = String::length;
    4         Stream<String> stream = Stream.of("程序员田螺", "捡田螺的小男孩", "捡瓶子的小男孩");
    5         Stream<Integer> resultStream = stream.map(function);
    6         resultStream.forEach(System.out::println);
    7     }

    Consumer<T>是一个消费性接口,通过传入参数,并且无返回的操作

    1    private void testComsumer() {
    2         //获取每个字符串的长度,并且返回
    3         Consumer<String> comsumer = System.out::println;
    4         Stream<String> stream = Stream.of("程序员田螺", "捡田螺的小男孩", "捡瓶子的小男孩");
    5         stream.forEach(comsumer);
    6     }

    Predicate<T>是一个判断型接口,并且返回布尔值结果.

    1     private void testPredicate() {
    2         //获取每个字符串的长度,并且返回
    3         Predicate<Integer> predicate = a -> a > 18;
    4         UserInfo userInfo = new UserInfo(2L, "程序员田螺", 27);
    5         System.out.println(predicate.test(userInfo.getAge()));
    6     }

    Supplier<T>是一个供给型接口,无参数,有返回结果。

    1     private void testSupplier() {
    2         Supplier<Integer> supplier = () -> Integer.valueOf("666");
    3         System.out.println(supplier.get());
    4     }

    这几个函数在日常开发中,也是可以灵活应用的,比如我们DAO操作完数据库,是会有个result的整型结果返回。我们就可以用Supplier<T>来统一判断是否操作成功。如下:

     1     private void saveDb(Supplier<Integer> supplier) {
     2         if (supplier.get() > 0) {
     3             System.out.println("插入数据库成功");
     4         }else{
     5             System.out.println("插入数据库失败");
     6         }
     7     }
     8     
     9     @Test
    10     public void add() throws Exception {
    11         Course course=new Course();
    12         course.setCname("java");
    13         course.setUserId(100L);
    14         course.setCstatus("Normal");
    15         saveDb(() -> courseMapper.insert(course));
    16     }
  • 相关阅读:
    python list介绍
    python unittest模块
    python 贪婪算法
    python 动态规划:背包问题
    汇编语言 基础知识(王爽)
    python 迪克斯特拉(Dijkstra)
    python 广度优先查找 (最短路径)
    Python 快速排序
    python 分而治之 找零数量 最小组合
    移动端的头部标签和 meta
  • 原文地址:https://www.cnblogs.com/midiyu/p/16841194.html
Copyright © 2020-2023  润新知