鱼C论坛

 找回密码
 立即注册
查看: 1784|回复: 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也同理 具体例子如下

package com.xiaoshangkou.MapTest;

import java.util.Map;
import java.util.Set;

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

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

        //遍历Map集合
        System.out.println("map集合");
        Set<Map.Entry<Integer,String>> set=map.entrySet();
        for (Map.Entry<Integer,String> entry:set){
            System.out.println(entry.getKey()+"="+entry.getValue());
        }
        System.out.println("");
        //遍历map1集合
        System.out.println("map1集合:");
        Set<Map.Entry<Integer,String>> set1=map1.entrySet();
        for (Map.Entry<Integer,String> entry:set){
            System.out.println(entry.getKey()+"="+entry.getValue());
        }
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

package com.xiaoshangkou.MapTest;

import java.util.Map;
import java.util.Set;

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

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

        //遍历Map集合
        System.out.println("map集合");
        Set<Map.Entry<Integer,String>> set=map.entrySet();
        for (Map.Entry<Integer,String> entry:set){
            System.out.println(entry.getKey()+"="+entry.getValue());
        }
        System.out.println("");
        //遍历map1集合
        System.out.println("map1集合:");
        Set<Map.Entry<Integer,String>> set1=map1.entrySet();
        for (Map.Entry<Integer,String> entry:set){
            System.out.println(entry.getKey()+"="+entry.getValue());
        }
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-7-8 13:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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