hashmap的几个重要函数

本文深入探讨了Java中HashMap的五个关键方法:computeIfAbsent、computeIfPresent、compute、merge及forEach。通过具体示例,展示了如何使用这些方法进行高效的数据处理,特别关注于在不重复构造对象的情况下更新Map集合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.V compute(K key,BiFunction<? super K,? super V,? extends V> remappingFunction)
2.V computeIfAbsent(K key,Function<? super K,? extends V> mappingFunction)
3.V computeIfPresent(K key,BiFunction<? super K,? super V,? extends V> remappingFunction)
4.V merge(K key,V value,BiFunction<? super V,? super V,? extends V> remappingFunction)
5.void forEach(BiConsumer<? super K,? super V> action)

写了个小例子,展示这几个函数的用法:

package halve;
 
import java.util.HashMap;
import java.util.LinkedList;
import java.util.function.BiConsumer;
 
/**
 * Created by fhqplzj on 16-7-18 at 上午9:24.
 */
public class Lotus {
    public static void main(String[] args) {
        String[] data = "a b c d a b c a b a".split(" ");
        /**
         * V computeIfAbsent(K key,Function<? super K,? extends V> mappingFunction)
         */
        HashMap<String, LinkedList<String>> map1 = new HashMap<>();
        for (String s : data) {
            map1.computeIfAbsent(s, s1 -> new LinkedList<>()).add(s);
        }
        System.out.println("map1 = " + map1);
        System.out.println();
        /**
         * V computeIfPresent(K key,BiFunction<? super K,? super V,? extends V> remappingFunction)
         */
        for (String s : data) {
            map1.computeIfPresent(s, (s1, strings) -> {
                System.out.println("key=" + s1 + ",value=" + strings);
                return strings;
            });
        }
        System.out.println("map1 = " + map1);
        System.out.println();
        /**
         * V compute(K key,BiFunction<? super K,? super V,? extends V> remappingFunction)
         */
        for (String s : data) {
            map1.compute(s, (s1, strings) -> {
                System.out.println("key=" + s1 + ",size=" + strings.size() + ",value=" + strings);
                return strings;
            }).add("x");
        }
        System.out.println("map1 = " + map1);
        System.out.println();
        /**
         * V merge(K key,V value,BiFunction<? super V,? super V,? extends V> remappingFunction)
         */
        HashMap<String, String> map2 = new HashMap<>();
        for (String s : data) {
            map2.merge(s, s, String::concat);
        }
        System.out.println("map2 = " + map2);
        System.out.println();
        HashMap<String, Integer> map3 = new HashMap<>();
        for (String s : data) {
            map3.merge(s, 1, (x, y) -> x + y);
        }
        System.out.println("map3 = " + map3);
        System.out.println();
        /**
         * void forEach(BiConsumer<? super K,? super V> action)
         */
        BiConsumer<String, Integer> biConsumer = (s, integer) -> System.out.println(s + " occurs " + integer + " times!");
        map3.forEach(biConsumer);
    }
}

href:https://blog.youkuaiyun.com/asd991936157/article/details/51939183

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值