JAVA8 MAP新增方法详解
1、compute
default V compute(K key,BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
尝试通过Lambda表达式重新计算给定KEY的映射值并更新MAP(值为null则删除KEY,否则重新写入)。
Map<String, String> tMap = new HashMap<String, String>() { { put("A", "AAA"); put("B", "BBB"); } }; tMap.compute("A", (k, v) -> v == null ? "AAA" : v.concat("AAA"));//KEY存在VALUE不为空且Lambda计算结果不为空,更新KEY System.out.println(tMap); tMap.compute("B", (k, v) -> v == null ? "BBB" : null);//KEY存在VALUE不为空但Lambda计算结果为空,删除KEY System.out.println(tMap); tMap.compute("C", (k, v) -> v == null ? "CCC" : v.concat("CCC"));//KEY不存在但Lambda计算结果不为空,新增KEY System.out.println(tMap);
2、computeIfAbsent
default V computeIfAbsent(K key,Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
如果给定的KEY为关联VALUE或关联到null,则尝试通过给定的Lambda函数计算其值并写入MAP(值为null则不写入)。
Map<String,String> map = new HashMap<>(); map.computeIfAbsent("A",k -> null); System.out.println(map); map.computeIfAbsent("B",k -> "BBB"); System.out.println(map);
3、computeIfPresent
default V computeIfPresent(K key,BiFunction<? super K,? super V,? extends V> remappingFunction)
If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
如果给定KEY存在映射值且非null,尝试通过Lambda表达式计算新值并更新MAP
If the function returns null, the mapping is removed. If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.
如果Lambda计算结果为null,则删除KEY。如果Lambda计算发生异常,则原映射关系不变。
Map<String, String> tMap = new HashMap<String, String>() { { put("A", "AAA"); put("B", "BBB"); } }; tMap.computeIfPresent("A", (k, v) -> v == null ? "AAA" : v.concat("AAA"));//Lambda计算结果不为空,更新KEY System.out.println(tMap); tMap.computeIfPresent("B", (k, v) -> v == null ? "BBB" : null);//Lambda计算结果为空,删除KEY System.out.println(tMap);
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
面向对象、工厂模式、构造函数、原型、原型链都不懂?看这一篇就够了!
本文是博主自己归纳曾学到的知识,以及分享工厂模式、构造函数、以及原型链之间的用途;有很多面试者都会遇到这些问题,但是一些小白来说,还是不清楚,这些知识点到底在技术中有什么用途,到底是干什么的?今天我就简单说一说,本篇博客会从基本开始介绍,如果你对今天的内容所致甚少,并且你有耐心看完,相信你会有超值的收获!!! 小白们都对原型链迷迷糊糊,经常查阅相关文章,却还是缕不清到底是干什么的。因为你不知道它到底有什么用,又是如何会用到它,所以在直接查阅原型链的时候,忽略了它之前很重要的知识,所以你不懂。这个很重要的知识,就是构造函数!!! 本篇博客要一环扣一环,决定帮大家捋清思路,所以我们从基本开始说,从头到尾请跟住博主的思路。 面向对象: 对象?无论在javascipt当中,还是java当中,或其他语言中,所提及的对象,绝对不是男女朋友的对象,所以我们凡人先忘掉感情视野,再体会一下这个词:“对象”。所谓对象,任何事物都可以称作是对象。比如: 医生给病人治疗,病人对于医生来说,就是治疗的对象。我们去商场购买苹果,苹果对于我们来说,就是要购买的对象。博主写的博客,你们就是我要展示博客的对象。科学家要...
- 下一篇
SpringBoot2.0 基础案例(08):集成Redis数据库,实现缓存管理
一、Redis简介 Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elasticsearch。这些案例整理好后,陆续都会上传Git。SpringBoot2 版本,支持的组件越来越丰富,对Redis的支持不仅仅是扩展了API,更是替换掉底层Jedis的依赖,换成Lettuce。本案例需要本地安装一台Redis数据库。 二、Spring2.0集成Redis 1、核心依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 2、配置文件 # 端口 server: port: 8008 spring: application: # 应用名称 name: node08-boot-redis # redis 配...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker快速安装Oracle11G,搭建oracle11g学习环境