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 分钟前
Pdf转Word案例(java)
java·pdf·word·格式转换
yuren_xia6 分钟前
Spring MVC中跨域问题处理
java·spring·mvc
计算机毕设定制辅导-无忧学长15 分钟前
ActiveMQ 源码剖析:消息存储与通信协议实现(二)
java·activemq·java-activemq
大G哥21 分钟前
用 Go 和 TensorFlow 实现图像验证码识别系统
开发语言·后端·golang·tensorflow·neo4j
一个憨憨coder30 分钟前
Spring 如何解决循环依赖问题?
java·后端·spring
钢铁男儿1 小时前
深入解析C#参数传递:值参数 vs 引用参数
java·开发语言·c#
努力努力再努力wz1 小时前
【c++深入系列】:万字详解vector(附模拟实现的vector源码)
运维·开发语言·c++·c
.YM.Z1 小时前
C语言——操作符
c语言·开发语言·算法
学渣676561 小时前
.idea和__pycache__文件夹分别是什么意思
java·ide·intellij-idea
yxc_inspire1 小时前
基于Qt的app开发第六天
开发语言·c++·qt