Java中怎么获取Map集合

发布时间:2022-04-01 16:02:23 作者:iii
来源:亿速云 阅读:114

这篇文章主要介绍了Java中怎么获取Map集合的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Java中怎么获取Map集合文章都会有所收获,下面我们一起来看看吧。

一、概述

二、创建Map集合的对象方式

public static void main(String[] args) {
        //创建Map集合对象
        Map<String,String> m=new HashMap<String,String>();
        //添加元素使用put方法,默认自然排序
        m.put("02","李四");
        m.put("04","赵六");
        m.put("01","张三");
        m.put("03","王五");
        System.out.println(m);
    }
}

三、Map集合的常用方法

方法名说明
V put(K key,V value)添加元素,添加重复键值元素会覆盖
V remove(Object key)根据键删除键值对元素
void clear()清除所有的键值对元素
Boolean containsKey(Object key)判断集合是否包含指定的键,包含返回true
Boolean containsValue(Object value)判断集合是否包含指定的值,包含返回true
Boolean isEmpty()判断集合是否为空
int size()获取集合的长度,也就是键值对的个数
public class MapDemo01 {
    public static void main(String[] args) {
        //创建Map集合对象
        Map<String,String> m=new HashMap<String,String>();
        //添加元素,put方法
        m.put("1","张三");
        m.put("2","李四");
        m.put("3","王五");
        m.put("4","赵六");
        // System.out.println(m);
        //根据键删除键值对元素
        System.out.println(m.remove("3"));//切记键是什么类型就写什么类型,不然会返回null
        System.out.println(m);
        //清除所有键值对元素
        m.clear();
        //Boolean isEmpty()判断集合是否为空
        System.out.println(m.isEmpty());
       // System.out.println(m);
        //Boolean containsKey(Object key);判断集合中是否包含指定的键
        System.out.println(m.containsKey("5"));//切记键是什么类型就写什么类型,不然会返回null
        //Boolean containsValue(Object value)判断集合是否包含指定的值,包含返回true
        System.out.println(m.containsValue("张三"));
        //int size()获取集合的长度,也就是键值对的个数
        System.out.println(m.size());
    }
}

四、Map的获取方法

方法名说明
V get(Object key)根据键获取值
Set<K>keySet()获取所有键的集合
Collection<V>values()获取所有值的集合
Set<Map.Entry<K,V>>entrySet()获取所有键值对对象的集合
public class MapDemo02 {
    public static void main(String[] args) {
        //创建Map对象
        Map<String,String> m=new HashMap<String,String>();
        //添加元素
        m.put("1","张三");
        m.put("3","李四");
        m.put("4","王五");
        m.put("2","赵六");
//        System.out.println(m);
        //V get(Object key)根据键获取值
        System.out.println(m.get("3"));//要注意键的类型,类型不对会报null
        //Set<K>keySet()获取所有键的集合,因为返回的是个集合,所以用增强for遍历
        Set<String> k=m.keySet();
        for (String key:k){
            System.out.println(key);
        }
        //Collection<V>values()获取所有值的集合,注意,他会按照键的排序对值进行排序
        Collection<String> c=m.values();
        for (String v:c){
            System.out.println(v);
        }

    }
}

五、Map集合的遍历方式

方式一:

public static void main(String[] args) {
        //方式一
        //创建Map集合对象
        Map<String,String> m=new HashMap<String,String>();
        //添加键值对
        m.put("1","张三");
        m.put("3","李四");
        m.put("4","王五");
        m.put("2","赵六");
        //获取所有键的集合
        Set<String>s=m.keySet();
        //遍历
        for (String key:s){
            //再通过键获取相对应的值
            String value=m.get(key);
            System.out.println(key+","+value);
        }
    }
}

方式二:

getKey()获取键

getValue()获取值

public static void main(String[] args) {
//        //方式一
//        //创建Map集合对象
//        Map<String,String> m=new HashMap<String,String>();
//        //添加键值对
//        m.put("1","张三");
//        m.put("3","李四");
//        m.put("4","王五");
//        m.put("2","赵六");
//        //获取所有键的集合
//        Set<String>s=m.keySet();
//        //遍历
//        for (String key:s){
//            //再通过键获取相对应的值
//            String value=m.get(key);
//            System.out.println(key+","+value);
//        }
        //方式二
        //创建Map集合对象
        Map<String,String> m=new HashMap<String,String>();
        //添加键值对
        m.put("1","张三");
        m.put("3","李四");
        m.put("4","王五");
        m.put("2","赵六");
        //获取所有键值对的集合Set<Map.Entry<K,V>>entrySet()
        Set<Map.Entry<String,String>> s= m.entrySet();
        //遍历该集合
        for (Map.Entry<String,String> ss:s){
            //通过键值对对象获取键值
            String key=ss.getKey();
            //通过键值对对象获取值
            String value=ss.getValue();
            System.out.println(key+","+value);

        }
    }
}

关于“Java中怎么获取Map集合”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Java中怎么获取Map集合”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Java使用keySet方法获取Map集合中的元素
  2. Java中Map集合的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java map

上一篇:Android基于Fresco怎么实现圆角和圆形图片

下一篇:C语言轮转数组问题怎么解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》