鱼C论坛

 找回密码
 立即注册
查看: 2381|回复: 1

[已解决]Collections里的方法能操作HashMap吗

[复制链接]
发表于 2022-1-11 22:03:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Collections里的方法能操作HashMap吗,如果能那么怎么用Collections里的addAll方法往HashMap里同时存入多个键值对
最佳答案
2022-1-12 11:05:14
不能 Collections是以单个方式存储元素的集合的超级父接口,而 HashMap是以键值对方式存储的集合
它的超级父接口是Map,所以他们的方法是不通用的,HashMap存储元素用到的是put方法
addAll方法是将指定集合中的所有元素添加到此集合。putAll也同理 具体例子如下

  1. package com.xiaoshangkou.MapTest;

  2. import java.util.Map;
  3. import java.util.Set;

  4. public class HashMap {
  5.     public static void main(String[] args) {
  6.         Map<Integer,String>map=new java.util.HashMap<>();
  7.         Map<Integer,String>map1=new java.util.HashMap<>();
  8.         map.put(1111,"zhangsan");
  9.         map.put(6666,"lisi");
  10.         map.put(7777,"wangwu");
  11.         map.put(2222,"zhaoliu");
  12.         map.put(2222,"king");//key重复的时候value会自动覆盖
  13.         map1.putAll(map);

  14.         System.out.println(map1.size());
  15.         System.out.println(map.size());

  16.         //遍历Map集合
  17.         System.out.println("map集合");
  18.         Set<Map.Entry<Integer,String>> set=map.entrySet();
  19.         for (Map.Entry<Integer,String> entry:set){
  20.             System.out.println(entry.getKey()+"="+entry.getValue());
  21.         }
  22.         System.out.println("");
  23.         //遍历map1集合
  24.         System.out.println("map1集合:");
  25.         Set<Map.Entry<Integer,String>> set1=map1.entrySet();
  26.         for (Map.Entry<Integer,String> entry:set){
  27.             System.out.println(entry.getKey()+"="+entry.getValue());
  28.         }
  29.     }
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-1-12 11:05:14 | 显示全部楼层    本楼为最佳答案   
不能 Collections是以单个方式存储元素的集合的超级父接口,而 HashMap是以键值对方式存储的集合
它的超级父接口是Map,所以他们的方法是不通用的,HashMap存储元素用到的是put方法
addAll方法是将指定集合中的所有元素添加到此集合。putAll也同理 具体例子如下

  1. package com.xiaoshangkou.MapTest;

  2. import java.util.Map;
  3. import java.util.Set;

  4. public class HashMap {
  5.     public static void main(String[] args) {
  6.         Map<Integer,String>map=new java.util.HashMap<>();
  7.         Map<Integer,String>map1=new java.util.HashMap<>();
  8.         map.put(1111,"zhangsan");
  9.         map.put(6666,"lisi");
  10.         map.put(7777,"wangwu");
  11.         map.put(2222,"zhaoliu");
  12.         map.put(2222,"king");//key重复的时候value会自动覆盖
  13.         map1.putAll(map);

  14.         System.out.println(map1.size());
  15.         System.out.println(map.size());

  16.         //遍历Map集合
  17.         System.out.println("map集合");
  18.         Set<Map.Entry<Integer,String>> set=map.entrySet();
  19.         for (Map.Entry<Integer,String> entry:set){
  20.             System.out.println(entry.getKey()+"="+entry.getValue());
  21.         }
  22.         System.out.println("");
  23.         //遍历map1集合
  24.         System.out.println("map1集合:");
  25.         Set<Map.Entry<Integer,String>> set1=map1.entrySet();
  26.         for (Map.Entry<Integer,String> entry:set){
  27.             System.out.println(entry.getKey()+"="+entry.getValue());
  28.         }
  29.     }
  30. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 05:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表