Collectors.toMap的对null友好实现
发表于|更新于
|浏览量:
令她反感的,远不是世界的丑陋,而是这个世界所戴的漂亮面具。——《不能承受的生命之轻》
我们在使用toMap时如果遇到null元素,经常会导致我们发生npe
很不方便
于是我给hutool提交了一个PR
完美解决了这个问题
使用方式:
升级到hutool-5.7.20
然后使用CollectorUtil
1 | Map<String, Integer> collect = Arrays.asList("ruben", "a chao", "vampire", "RUBEN", "VAMPIRE", null).stream().collect(CollectorUtil.toMap(Function.identity(), String::length, (l, r) -> l)); |
即可
相关推荐
2024-07-06
在嵌入式tomcat运行如何添加spring配置配置参数
placehloder 我们知道,在springboot用jar运行时,可以通过指定参数来进行配置 1java -jar myapp.jar --server.port=8081 --spring.datasource.url=jdbc:mysql://localhost:3306/mydb 如果是tomcat运行,则有下列几种方式 在 CATALINA_OPTS 环境变量中设置参数: 你可以在 Tomcat 的环境变量 CATALINA_OPTS 中添加 Spring 配置参数。例如,编辑 setenv.sh(Linux)或 setenv.bat(Windows)文件,添加以下内容: 12# setenv.sh (Linux)export CATALINA_OPTS="$CATALINA_OPTS -Dserver.port=8081 -Dspring.datasource.url=jdbc:mysql://localhost:3306/mydb" 12rem setenv.bat (Windows)set CATALINA_OPTS=%CATAL...
2023-01-24
spring caching
凡事必须要有统一和决断,因此,胜利不站在智慧的一方,而站在自信的一方。——拿破仑 官方文档:https://spring.io/guides/gs/caching/ 引入依赖: 1234<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId></dependency> 准备一个Book类 12345678910111213141516171819202122232425262728293031323334package com.example.caching;public class Book { private String isbn; private String title; public Book(String isbn, String title) { this.isbn = isbn; ...
2023-01-25
Cacheable CacheEvict CachePut
看书和学习是思想的经常营养,是思想的无穷发展——冈察洛夫 昨天写了spring caching简单入门 今天把省下俩注解也说了 一共是 @Cacheable加缓存(缓存获取不到就调用方法获取结果再放入缓存) @CachePut更新缓存,我下方的用法有误,应该和其余俩注解应用的方法参数保持一致,见后续博客 @CacheEvict删缓存 我们在Repository实现类加上这几个缓存注解 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950package com.ruben.simplecache;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.CachePut;import org.spring...
2023-02-12
h2换hsqldb最新版报错
沉溺于追求物质享受,就等于给飞鸟的双翅系上了黄金——佚名 直接改依赖: 123456789101112<!--<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>${h2.version}</version> <scope>test</scope></dependency>--><dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>2.7.1</version> <scope>test</scope></dependency> 随便运...
2024-03-16
mybatis的@MappedTypes
“Given enough eyeballs, all bugs are shallow.” — Linus’s Law, Eric S. Raymond 看到com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler的源码 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778/* * Copyright (c) 2011-2023, baomidou (jobob@qq.com). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * ...
2023-12-04
自动拆箱调用方法原理
没有知识,我们就谈不到欣赏。——别林斯基 今天分享一个冷门基础小知识 我们一般使用boolean的类型,使用结构控制语句条件表达式的时候都是这样: 12if (bar) {} 此处如果是一个基本类型,也就是boolean的时候,完全ok 但如果是一个包装类型的java.lang.Boolean对象,则在bar为null的时候,抛出 1java.lang.NullPointerException 这是因为底层调用了java.lang.Boolean#valueOf(boolean)拆箱 此处 1234@IntrinsicCandidatepublic static Boolean valueOf(boolean b) { return (b ? TRUE : FALSE);} 由于添加了@IntrinsicCandidate注解,所以会调用其进行自动拆箱 因此如果是Boolean包装类型,则调用 12if (bar) {} 等同于 12if (Boolean.valueOf(bar)) {...

阿超
我的名字叫阿超 年龄25岁 家在北京市 职业是软件开发 每天最晚也会在八点前回家 不抽烟 酒浅尝辄止 晚上十二点上床 保证睡足八个小时 睡前写一篇博客 再做二十分钟俯卧撑暖身 然后再睡觉 基本能熟睡到天亮 像婴儿一样不留下任何疲劳和压力 就这样迎来第二天的早晨 健康检查结果也显示我很正常 我想说明我是一个不论何时都追求内心平稳的人 不拘泥于胜负 不纠结于烦恼 不树立使我夜不能寐的敌人 这就是我在这社会的生活态度
Follow Me公告
This is my Blog