hashMap一些不太常用但非常有用的方法及使用示例

HashMap 提供了多种方法来操作键值对,除了常用的 put()、get() 和 remove() 方法之外,还有一些不太常用但非常有用的方法。下面是几个不常用但很有用的 HashMap 方法及其使用示例:

  1. compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)

此方法允许你基于现有值(如果存在)或计算新值,并将其放入映射中。

示例:

java 复制代码
import java.util.HashMap;
import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("apple", 1);
        
        // 如果有值,则增加1;如果没有,则设置为10
        BiFunction<String, Integer, Integer> function = (key, oldValue) -> oldValue == null ? 10 : oldValue + 1;
        map.compute("apple", function);
        System.out.println(map.get("apple")); // 输出: 2
        
        map.compute("banana", function);
        System.out.println(map.get("banana")); // 输出: 10
    }
}
  1. computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)

当指定键没有对应值时,使用提供的映射函数计算其值并放入映射中。

示例:

java 复制代码
import java.util.HashMap;
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        
        Function<String, Integer> mappingFunction = key -> key.length();
        Integer value = map.computeIfAbsent("apple", mappingFunction);
        System.out.println(value); // 输出: 5
        
        // 已存在的键不会重新计算
        value = map.computeIfAbsent("apple", mappingFunction);
        System.out.println(value); // 输出: 5
    }
}
  1. computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)

只有在键对应的值非空时,才应用给定的重映射函数计算新值。

示例:

java 复制代码
import java.util.HashMap;
import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("apple", 1);
        
        BiFunction<String, Integer, Integer> remappingFunction = (key, oldValue) -> oldValue + 1;
        map.computeIfPresent("apple", remappingFunction);
        System.out.println(map.get("apple")); // 输出: 2
        
        // 对于不存在的键,不会执行任何操作
        map.computeIfPresent("banana", remappingFunction);
        System.out.println(map.containsKey("banana")); // 输出: false
    }
}
  1. merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)

如果指定的键尚未与某个值关联或关联的值为 null,则将该键与给定的值关联;否则,使用给定的重映射函数重新计算该键的新值。

示例:

java 复制代码
import java.util.HashMap;
import java.util.function.BiFunction;

public class Main {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("apple", 1);
        
        BiFunction<Integer, Integer, Integer> remappingFunction = Integer::sum;
        map.merge("apple", 2, remappingFunction);
        System.out.println(map.get("apple")); // 输出: 3
        
        map.merge("banana", 5, remappingFunction);
        System.out.println(map.get("banana")); // 输出: 5
    }
}
相关推荐
伍六星1 分钟前
更新Java的环境变量后VScode/cursor里面还是之前的环境变量
java·开发语言·vscode
Dola_Pan4 分钟前
Android四大组件通讯指南:Kotlin版组件茶话会
android·开发语言·kotlin
风象南7 分钟前
SpringBoot实现简易直播
java·spring boot·后端
万能程序员-传康Kk16 分钟前
智能教育个性化学习平台-java
java·开发语言·学习
道剑剑非道20 分钟前
QT开发技术【ffmpeg + QAudioOutput】音乐播放器
开发语言·qt·ffmpeg
落笔画忧愁e26 分钟前
扣子Coze飞书多维表插件-列出全部数据表
java·服务器·飞书
@残梦27 分钟前
129、QT搭建FFmpeg环境
开发语言·qt·ffmpeg
鱼儿也有烦恼29 分钟前
Elasticsearch最新入门教程
java·elasticsearch·kibana
eternal__day39 分钟前
微服务架构下的服务注册与发现:Eureka 深度解析
java·spring cloud·微服务·eureka·架构·maven
序属秋秋秋41 分钟前
《C++初阶之类和对象》【命名空间 + 输入&输出 + 缺省参数 + 函数重载】
开发语言·c++·笔记