一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Java基础Map集合代码解析

时间:2022-04-01 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下Java基础Map集合代码解析,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

一、概述

Interface Map k:键的类型;V:值的类型

将键映射到值的对象;不能包含重复的键;每个键可以映射到最多一个值

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

1.使用多态的方式

2.具体实现类HashMap

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

三、Map集合的常用方法

public class MapDemo01 {
    public static void main(String[] args) {
        //创建Map集合对象
        Map m=new HashMap();
        //添加元素,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的获取方法

public class MapDemo02 {
    public static void main(String[] args) {
        //创建Map对象
        Map m=new HashMap();
        //添加元素
        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
        //SetkeySet()获取所有键的集合,因为返回的是个集合,所以用增强for遍历
        Set k=m.keySet();
        for (String key:k){
            System.out.println(key);
        }
        //Collectionvalues()获取所有值的集合,注意,他会按照键的排序对值进行排序
        Collection c=m.values();
        for (String v:c){
            System.out.println(v);
        }

    }
}

五、Map集合的遍历方式

方式一:

1.先获取Map集合中的所有键的集合,使用方法setKey()

2.遍历所有键的集合获取每一个键

3.通过每一个键获取相对应的值 getValues方法

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

方式二:

1.获取所有键值对的集合,使用Set>entrySet()方法

2.遍历这个集合获得每一个键值对 对象也就是Map.Entry对象

3.再根据键值对对象获取值和键

getKey()获取键

getValue()获取值

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

        }
    }
}

热门栏目