【Map】containsKey和containsValue使用案例(114)

news/2025/3/14 22:14:10/

containsKey() 是看有没有这个键,也就是有没有这个下标
containsValue() 是看有没有这个键值,也就是有没有这个值
比如
mp.add (10,20);
containsKey (10) 是 true
containsValue (10) 是 false
而containsValue (20) 是 true
区别就是:
前者看没有这个键(下标)后者有没有这个键值(值)

Demo案例


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;/**  containsKey() 是看有没有这个键,也就是有没有这个下标containsValue() 是看有没有这个键值,也就是有没有这个值比如mp.add (10,20);containsKey (10) 是 truecontainsValue (10) 是 false而containsValue (20) 是 true区别就是:前者看没有这个键(下标)后者有没有这个键值(值)*/public class day05 {public static void main(String[] args) {List<Map<String,Object>> list = new ArrayList<>();Map<String,Object> map1 = new HashMap<>();map1.put("region","西安");map1.put("num",3.3);Map<String,Object> map2 = new HashMap<>();map2.put("region","上海");map2.put("num",4.3);Map<String,Object> map3 = new HashMap<>();map3.put("region","北京");map3.put("num",5.4);Map<String,Object> map4 = new HashMap<>();map4.put("region","西安");map4.put("num",6.4);Map<String,Object> map5 = new HashMap<>();map5.put("region","西安");map5.put("num",6.1);Map<String,Object> map6 = new HashMap<>();map6.put("region","北京");map6.put("num",6.4);list.add(map1);list.add(map2);list.add(map3);list.add(map4);list.add(map5);list.add(map6);	System.out.println("list:"+list);Map<String,Object> regionMap = new HashMap<>();	for (int i = 0; i < list.size(); i++) {String num = String.valueOf(list.get(i).get("num"));String region = String.valueOf(list.get(i).get("region")).trim();if(!num.contains(".")){list.get(i).put("num","0.0");}String num2 = String.valueOf(list.get(i).get("num"));// 如果regionMap已经存在List中的map,则value值相加;否则将List中的map添加到regionMap中;if(regionMap.containsKey(region)) {String key = region;String value = String.valueOf(regionMap.get(key)); regionMap.put(region, Double.parseDouble(value) + Double.parseDouble(num2));}else {regionMap.put(region, num2);}}System.out.println("regionMap:"+regionMap);}
}

测试输出:

list:[{num=3.3, region=西安}, {num=4.3, region=上海}, {num=5.4, region=北京}, {num=6.4, region=西安}, {num=6.1, region=西安}, {num=6.4, region=北京}]
regionMap:{上海=4.3, 西安=15.799999999999999, 北京=11.8}

http://www.ppmy.cn/news/1040562.html

相关文章

Docker+Selenium Grid搭建自动化测试平台

安装docker yum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager –add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install docker-ce -y Create a Docker Network docker network create grid 下载镜像 hu…

18万字应急管理局智慧矿山煤矿数字化矿山技术解决方案WORD

导读&#xff1a;原文《18万字应急管理局智慧矿山煤矿数字化矿山技术解决方案WORD》&#xff08;获取来源见文尾&#xff09;&#xff0c;本文精选其中精华及架构部分&#xff0c;逻辑清晰、内容完整&#xff0c;为快速形成售前方案提供参考。 目 录 第一章 项目概述 1.1项目…

Vue中Hottable的使用

安装 npm install handsontable handsontable/vue引入 <template><HotTable :root"root" :data"tableValue" ref"testHot" :settings"hotSettings"></HotTable> </template><script> import HotTable…

基于java远程同步课堂系统设计与实现

摘要&#xff1a;在这样一个网络数据大爆炸的时代&#xff0c;人们获取知识、获取信息的通道非常的多元化&#xff0c;通过网络来实现数据信息的获取成为了现在非常常见的一种方式&#xff0c;而通过网络进行教学&#xff0c;在网络上进行远程的课堂培训&#xff0c;能够根据学…

ElasticSearch的客户端操作

ElasticSearch的客户端操作 1、客户端介绍 官方文档地址&#xff1a; https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html 实际开发中&#xff0c;有多种方式操作Elasticsearch&#xff1a; 客户端工具&#xff1a;发送http请求(RESTful风格)操作…

DataFrame.rename()函数--Pandas

1. 函数作用 修改DataFrame的行名、列名 2. 函数语法 DataFrame.rename(mapperNone, *, indexNone, columnsNone, axisNone, copyNone, inplaceFalse, levelNone, errorsignore)3. 函数参数 参数含义mapper与axis结合使用&#xff0c;表示运用到axis上的值&#xff1a;类字…

【高危】企业微信私有化2.5-2.6.93版本后台API未授权访问漏洞

漏洞描述 企业微信私有化2.5.x版本及2.6.930000版本以下后台中存在接口未授权访问漏洞&#xff0c;攻击者通过访问/cgi-bin/gateway/agentinfo接口可获得Secret&#xff0c;从而利用开放API获取企业通讯录等敏感信息及企业微信内应用权限。 漏洞名称企业微信私有化2.5-2.6.93…

uniapp 顶部头部样式

<u-navbartitle"商城":safeAreaInsetTop"true"><view slot"left"><image src"/static/logo.png" mode"" class"u-w-50 u-h-50"></image></view></u-navbar>