Java教程

Map

本文主要是介绍Map,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Map集合概述

Interface Map<K,V> K:键的类型;V:值的类型
Map是键值对的一一映射关系,键具有唯一性
MapDemo1.java

    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;

    public class MapDemo01 {
        public static void main(String[] args) {
            Map<String, String> map = new HashMap<>();
            map.put("01","张三");
            map.put("02","李四");
            map.put("03","王五");
            map.put("04","赵六");
            map.put("05","张麻子");

            System.out.println(map);
            System.out.println("-----------");
    //        {01=张三, 02=李四, 03=王五, 04=赵六, 05=张麻子}
            //遍历Map集合
            // 获取所有键的集合
            Set<String> keySet =map.keySet();
            for (String Key: keySet){
                System.out.println(Key);
            }
            System.out.println("-----------");
            //遍历map集合
            for (String key :keySet){
                String value =map.get(key);
                System.out.println(key+"    ,   " +value );
            }
            System.out.println("-----------");
            //获取所有值的集合
            Collection<String> values =map.values();
            for (String value : values){
                System.out.println(value);
            }

            System.out.println("-----------");
            //获取所有键值对对象的集合
            Set<Map.Entry<String, String>> entrySet = map.entrySet();
            for (Map.Entry<String,String> me :entrySet){
                String key = me.getKey();
                String value=me.getValue();
                System.out.println(key +"   ,   "+value);
            }
        }
    }

HashMapDemo.java

    import set.Student;

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

    public class HashMapDemo  {
        public static void main(String[] args) {
            HashMap<String,Student> hm=new HashMap<String,Student>();

            Student s1=new Student("张三",22);
            Student s2=new Student("李四",23);
            Student s3=new Student("王五",21);
            Student s4=new Student("赵六",23);
            hm.put("01",s1);
            hm.put("02",s2);
            hm.put("03",s3);
            hm.put("04",s4);

            //键找值
            Set<String> key = hm.keySet();
            for (String k : key){
                Student value =hm.get(k);
                System.out.println(k+ " ,   "+value.getName());
            }

            System.out.println("-------------");
            //键值对对象
            Set<Map.Entry<String, Student>> entries = hm.entrySet();
            for (Map.Entry<String,Student> me :entries){
                String k=me.getKey();
                Student value =me.getValue();
                System.out.println(k+ " ,   "+value.getName());
            }

        }

    }

实例:要求创建一个HashMap集合,键是学生对象(Student),值是居住地(String),存储多个键值对,并遍历。要求保证键的唯一性,如果学生对象的成员变量值相同,我们就认为是同一对象。
hashMapDemo02.java

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

    public class hashMapDemo02 {
        public static void main(String[] args) {
            HashMap<Student, String> hm = new HashMap<>();
            Student s1 =new Student("张三",23);
            Student s2 =new Student("李四",22);
            Student s3 =new Student("王五",21);
            Student s4 =new Student("赵六",23);
            Student s5 =new Student("张三",23);

            hm.put(s1,"北京");
            hm.put(s2,"重庆");
            hm.put(s3,"上海");
            hm.put(s4,"成都");
            hm.put(s5,"深圳");
            //后面覆盖前面

            Set<Map.Entry<Student, String>> entrySet = hm.entrySet();
            for (Map.Entry<Student,String> stu : entrySet)
            {
                Student student= stu.getKey();
                String address=stu.getValue();
                System.out.println(student.getName()+","+student.getAge()+";"+address);
            }

        }
    }
这篇关于Map的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!