美文网首页
java8 map - 新特性常用方法

java8 map - 新特性常用方法

作者: 一点温柔 | 来源:发表于2021-02-20 18:03 被阅读0次

前言

java8中对map的使用新增了一些十分有用的方法,以下列举部分常用methed()

常用方法解析

1、compute()

参数格式:
compute(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction)

简要说明:
不在乎当前item的旧值,直接以remappingFunction()结果的新值为准,如果新值为空则移除当前item.

源码分析:

default V compute(K key,
            BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
        Objects.requireNonNull(remappingFunction);
        V oldValue = get(key);

        V newValue = remappingFunction.apply(key, oldValue);
        if (newValue == null) {
            // delete mapping
            if (oldValue != null || containsKey(key)) {
                // something to remove
                remove(key);
                return null;
            } else {
                // nothing to do. Leave things as they were.
                return null;
            }
        } else {
            // add or replace old mapping
            put(key, newValue);
            return newValue;
        }

使用格式:

testMap.compute("key1", (v1, v2) -> v2);

使用场景:
put()方法的增强使用

2、computeIfPresent()

参数格式:
computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)

简要说明:
计算item方法, 入参为item的key、remappingFunction
如果原来的key有值,且不为null,那么将remappingFunction中的新值给key,新值如果为空,移除当前key的item
如果原来key没值,直接给null

源码分析:

default V computeIfPresent(K key,
            BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
        Objects.requireNonNull(remappingFunction);
        V oldValue;
        if ((oldValue = get(key)) != null) {
            V newValue = remappingFunction.apply(key, oldValue);
            if (newValue != null) {
                put(key, newValue);
                return newValue;
            } else {
                remove(key);
                return null;
            }
        } else {
            return null;
        }

使用格式:

testMap.computeIfPresent("key1", (v1, v2) -> "test");

使用场景:
put()方法的增强使用

3、computeIfAbsent()

参数格式: computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)

简要说明:
计算item方法,入参为item的key、remappingFunction
如果当前key旧值不为空,那么以旧值为准
如果当前key旧值为空,以function结果新值为准,新值为空则remove当前item

源码分析:

default V computeIfAbsent(K key,
            Function<? super K, ? extends V> mappingFunction) {
        Objects.requireNonNull(mappingFunction);
        V v;
        if ((v = get(key)) == null) {
            V newValue;
            if ((newValue = mappingFunction.apply(key)) != null) {
                put(key, newValue);
                return newValue;
            }
        }

        return v;
    }

使用格式:

tempEntityRouteMap.computeIfAbsent(entityType, k -> TreeRangeMap.create());

使用场景:
put()方法的增强使用
未完待续......

相关文章

  • java8 map - 新特性常用方法

    前言 java8中对map的使用新增了一些十分有用的方法,以下列举部分常用methed() 常用方法解析 1、co...

  • java8Optional

    最近用java8新特性optional遇到里面有2个方法,map和flatMap,特此记录 1、map中获取的返回...

  • Java8常用的新特性总结

    一、Java8常用的新特性总结 1.1.Java8常用特性总览 1.2.lambda表达式 在Java8中引入了一...

  • 1.Java8你需要了解的

    一、Java8新增了哪些新特性: 新特性Java8 新增了非常多的特性,我们主要讨论以下几个: 1、接口默认方法:...

  • Java8特性之Lambda、方法引用和Streams

    Java8特性之Lambda、方法引用和Streams Java8已经推出了好一段时间了,而掌握Java8的新特性...

  • map和flatMap区别

    map和flatMap都是Java8新语法stream的方法,map很常用,就是传入一个元素,返回1个元素(nul...

  • java 各版本新特性介绍

    java8 新特性 Java8 主要包括的新特性有: 函数式接口如果一个接口只有一个抽象方法,那么该接口就成为一个...

  • Java发展历史

    Java8新特性:

  • Java8 新特性

    Java8新特性

  • java8 新特性总结

    java8新特性 1.接口方法默认实现:java8中接口方法可以有默认实现,需要加入default关键字。 2.L...

网友评论

      本文标题:java8 map - 新特性常用方法

      本文链接:https://www.haomeiwen.com/subject/aikxfltx.html