Java中映射Map的merge、compute、computeIfAbsent、computeIfPresent基本用法

4 篇文章 0 订阅
1 篇文章 0 订阅

下面是Java8中Map的一些新方法merge、compute、computeIfAbsent、computeIfPresent介绍。

我们在项目开发中,经常使用map,key有时存在有时不存,我们需要用containsKey方法进行判断,然后再决定如何修改value。 这样比较麻烦。能不能在一个方法调用就完成这些工作呢(如果key存在value(还可以有其他逻辑判断),就do a,如果不存在就do b)?答案是, 当然可以。

下面直接给出示例和运行结果,参看结果就明白各个方法的具体含义了。

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

/**
 * @author yqbjtu
 * @data 2018年3月31日 下午6:24:16
 * @version 1.0
 *
 */
public class MapMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Map<String, String> myMap = new HashMap<>();
		String keyA = "A";
		String keyB = "B";
		String keyC = "C";
		String keyD = "D";
		String keyE = "E";
		String keyF = "F";
		String keyG = "G";
		String keyH = "H";
		myMap.put(keyA, "str01A");
		myMap.put(keyB, "str01B");
		myMap.put(keyC, "str01C");
		
		System.out.println("myMap initial content:"+ myMap);
		
		myMap.merge(keyA, "merge01", String::concat);
		myMap.merge(keyD, "merge01", String::concat);
		System.out.println("Map merge demo content:"+ myMap);
		
		BiFunction<String, String, String> biFunc = new BiFunction<String, String, String>(){
            @Override
            public String apply(String t, String u) {
            	String result = t;
            	if (t == null) {
            		result = u;
            	}
            	else {
            		result += "," + u;
            	}
                return result;
            }
        };
        
		myMap.merge(keyA, "BiFuncMerge01", biFunc);
		myMap.merge(keyE, "BiFuncMerge01", biFunc);
		System.out.println("Map customized BiFunction merge demo content:"+ myMap);
		
		String msg = "msgCompute";
		myMap.compute(keyB, (k, v) -> (v == null) ? msg : v.concat(msg));
		myMap.compute(keyF, (k, v) -> (v == null) ? msg : v.concat(msg));
		System.out.println("Map customized BiFunction compute demo content:"+ myMap);
		
		myMap.computeIfAbsent(keyC, k -> genValue(k));
		myMap.computeIfAbsent(keyG, k -> genValue(k));
		System.out.println("Map customized Function computeIfAbsent demo content:"+ myMap);
		
		myMap.computeIfPresent(keyC, biFunc);
		myMap.computeIfPresent(keyH, biFunc);
		System.out.println("Map customized biFunc computeIfPresent demo content:"+ myMap);
	}
	
	static String genValue(String str) {  
        System.out.println("===");  
        return str + "2";  
    }  
}

运行结果

myMap initial content:{A=str01A, B=str01B, C=str01C}
Map merge demo content:{A=str01Amerge01, B=str01B, C=str01C, D=merge01}
Map customized BiFunction merge demo content:{A=str01Amerge01,BiFuncMerge01, B=str01B, C=str01C, D=merge01, E=BiFuncMerge01}
Map customized BiFunction compute demo content:{A=str01Amerge01,BiFuncMerge01, B=str01BmsgCompute, C=str01C, D=merge01, E=BiFuncMerge01, F=msgCompute}
===
Map customized Function computeIfAbsent demo content:{A=str01Amerge01,BiFuncMerge01, B=str01BmsgCompute, C=str01C, D=merge01, E=BiFuncMerge01, F=msgCompute, G=G2}
Map customized biFunc computeIfPresent demo content:{A=str01Amerge01,BiFuncMerge01, B=str01BmsgCompute, C=C,str01C, D=merge01, E=BiFuncMerge01, F=msgCompute, G=G2}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值