Java集合之Map集合

定义一个Map集合
Map<String,Integer> map=new HashMap<String,Integer>();

//map中key不同,value可以相同
map.put("a",1);
map.put("b",1);
map.put("c",1);
System.out.println(map);

//根据key来取value
System.out.println("获取b的value为:"+map.get("b"));

//根据key来移除value
map.remove("c");
System.out.println("移除c后的map集合为:"+map);

//获取集合的长度
System.out.println("map集合的长度为:"+map.size());

//判断当前map集合是否包含指定的key
System.out.println(map.containsKey("a"));

//判断当前map集合是否包含指定的Value
System.out.println(map.containsValue(1));

//获取map集合的key的集合
System.out.println(map.keySet());

//获取map集合的所有value值
System.out.println(map.values());

//遍历map集合,通过map.keySet();
Set<String> keys = map.keySet();
System.out.println(keys+"11111111111111111111");
for (String key: keys){
System.out.println("key:"+key+","+"value:"+map.get(key));
}

//通过map.entrySet();遍历map集合
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (Map.Entry<String,Integer> en:entries){
System.out.println("key:"+en.getKey()+","+"value:"+map.get(en.getValue()));
}
posted @   SmallPepsi  阅读(106)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示