JAVA对map的所有键(key)值统一转换成小写字母和大写字母,超实用工具类(亲测)

你也可以访问这个地址继续学习:https://www.processon.com/view/6076a7ffe401fd2d66980f5b

更多技术学习请到:https://www.processon.com/view/60504b5ff346fb348a93b4fa

1、转换前数据截图:

2、转换成功截图:

3、废话不多说,直接上代码

     // main方法运行测试
     public static void main(String[] args) {

		Map<String, Object> m3 = new HashMap<String, Object>();
		m3.put("a", "abc");
		m3.put("b", "123");
		m3.put("C", "123");
		m3.put("dD", "123");

		Map<String, Object> m4 = new HashMap<String, Object>();
		m4.put("b", "123");
		m4.put("a", "abc");
		m4.put("cDs", "123");
		m4.put("d", "123");
		
		System.out.println("将m4数据的key全部转换为大写===" + transformUpperCase(m4));
		System.out.println("将m3数据的key全部转换为小写===" + transformLowerCase(m4));

	}

	// 将map值全部转换为大写
	public static Map<String, Object> transformUpperCase(Map<String, Object> orgMap) {
		Map<String, Object> resultMap = new HashMap<>();
		if (orgMap == null || orgMap.isEmpty()) {
			return resultMap;
		}
		Set<String> keySet = orgMap.keySet();
		for (String key : keySet) {
			String newKey = key.toUpperCase();
//			newKey = newKey.replace("_", "");
			resultMap.put(newKey, orgMap.get(key));
		}
		return resultMap;
	}

	// 将map值全部转换为小写
	public static Map<String, Object> transformLowerCase(Map<String, Object> orgMap) {
		Map<String, Object> resultMap = new HashMap<>();
		if (orgMap == null || orgMap.isEmpty()) {
			return resultMap;
		}
		Set<String> keySet = orgMap.keySet();
		for (String key : keySet) {
			String newKey = key.toLowerCase();
//			newKey = newKey.replace("_", "");
			resultMap.put(newKey, orgMap.get(key));
		}
		return resultMap;
	}

转载注明出处:https://blog.csdn.net/qq_28245087/article/details/86652602

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值