• Java—Map


    目录

    1.Map

    1.1Map集合概述和使用

    1.2Map集合的基本功能

    1.3Map集合的获取功能

    1.4Map集合的遍历

    1.5Map集合的遍历(方式2)

    案例:HashMap集合存储学生对象并遍历

    案例:HashMap集合存储学生对象并遍历


    1.Map

    1.1Map集合概述和使用

    Map集合概述
            InterfaceMapK键的类型;V值的类型
            将键映射到值的对象;不能包含重复的键;每个键可以映射到最多一个值

            

    创建Map集合的对象
            多态的方式
            具体的实现类HashMap

    1. package zyy07;
    2. import java.util.HashMap;
    3. import java.util.Map;
    4. import java.util.TreeSet;
    5. public class Demo {
    6. public static void main(String[] args) {
    7. //创建集合对象
    8. Map map=new HashMap();
    9. //put
    10. map.put("0124","zyy");
    11. map.put("0828","jw");
    12. map.put("0828","jwei");//输出这个,因为它会替代前一个
    13. map.put("1014","hwj");
    14. System.out.println(map);
    15. }
    16. }

    1.2Map集合的基本功能

    1. package zyy07;
    2. import java.util.HashMap;
    3. import java.util.Map;
    4. import java.util.TreeSet;
    5. public class Demo {
    6. public static void main(String[] args) {
    7. //创建集合对象
    8. Map map=new HashMap();
    9. //put
    10. map.put("0124","zyy");
    11. map.put("0828","jw");
    12. map.put("0828","jwei");//输出这个,因为它会替代前一个
    13. map.put("1014","hwj");
    14. System.out.println(map);
    15. //remove
    16. System.out.println(map.remove("0124"));
    17. //System.out.println(map.remove(0810));
    18. //clear
    19. //map.clear();
    20. //containsKey
    21. System.out.println(map.containsKey("0828"));
    22. System.out.println(map.containsKey("0206"));
    23. //containsValue
    24. System.out.println(map.containsValue("hwj"));
    25. System.out.println(map.containsValue("zyy"));
    26. //isEmpty
    27. System.out.println(map.isEmpty());
    28. //size
    29. System.out.println(map.size());
    30. System.out.println(map);
    31. }
    32. }

    1.3Map集合的获取功能

    1. package zyy07;
    2. import java.util.*;
    3. public class Demo {
    4. public static void main(String[] args) {
    5. //创建集合对象
    6. Map map=new HashMap();
    7. //put
    8. map.put("0124","zyy");
    9. map.put("0828","jw");
    10. map.put("0828","jwei");//输出这个,因为它会替代前一个
    11. map.put("1014","hwj");
    12. System.out.println(map);
    13. //get
    14. System.out.println(map.get("0124"));
    15. //setkeySet
    16. Set ky=map.keySet();
    17. for(String s:ky){
    18. System.out.println(s);
    19. }
    20. //Collection values
    21. Collection v=map.values();
    22. for(String s:v){
    23. System.out.println(s);
    24. }
    25. }
    26. }

     1.4Map集合的遍历

    我们刚才存储的元素都是成对出现的,所以我们把Map看成是一个夫妻对的集合

    遍历思路
            把所有的丈夫给集中起来
            遍历丈夫的集合,获取到每一个丈夫

            根据丈夫去找对应的妻子

    转换为Map集合中的操作:
            获取所有键的集合。用kevSet()方法实现
            遍历键的集合,获取到每一个键。用增强for实现

            根据键去找值。用get(Obiectkev)方法实现

    1. package zyy07;
    2. import java.util.*;
    3. public class Demo {
    4. public static void main(String[] args) {
    5. //创建集合对象
    6. Map map=new HashMap();
    7. //put
    8. map.put("0124","zyy");
    9. map.put("0828","jw");
    10. map.put("0828","jwei");//输出这个,因为它会替代前一个
    11. map.put("1014","hwj");
    12. System.out.println(map);
    13. //setkeySet
    14. Set ky=map.keySet();
    15. for(String s:ky){
    16. String v=map.get(s);
    17. System.out.println(s+","+v);
    18. }
    19. }
    20. }

    1.5Map集合的遍历(方式2)

    我们刚才存储的元素都是成对出现的,所以我们把Map看成是一个夫妻对的集合

    遍历思路
            获取所有结婚证的集合
            遍历结婚证的集合,得到每一个结婚证

            根据结婚证获取丈夫和妻子

    转换为Map集合中的操作:
            获取所有键值对对象的集合
            Set> entrySet():获取所有键值对对象的集合

    遍历键值对对象的集合,得到每一个键值对对象
            用增强for实现,得到每一个Map.Entry

    根据键值对对象获取键和值
            用getKey0得到键

            用getValue()得到值

    1. package zyy07;
    2. import java.util.*;
    3. public class Demo {
    4. public static void main(String[] args) {
    5. //创建集合对象
    6. Map map=new HashMap();
    7. //put
    8. map.put("0124","zyy");
    9. map.put("0828","jw");
    10. map.put("0828","jwei");//输出这个,因为它会替代前一个
    11. map.put("1014","hwj");
    12. System.out.println(map);
    13. Set> e = map.entrySet();
    14. for(Map.Entry m:e){
    15. String s1=m.getKey();
    16. String s2=m.getValue();
    17. System.out.println(s1+","+s2);
    18. }
    19. }
    20. }

    案例:HashMap集合存储学生对象并遍历

    需求:创建一个HashMap集合,键是学号(String),值是学生对象(Student)。存储三个键值对元素,并遍历

    思路:
    ①定义学生类
    ②创建HashMap集合对象
    ③ 创建学生对象 
    ④把学生添加到集合
    ⑤遍历集合
            方式1:键找值
            方式2:键值对对象找键和值

    学生类:

    1. package com.test;
    2. public class student {
    3. //成员变量
    4. private String name;
    5. private int age;
    6. //构造方法
    7. public student(){
    8. }
    9. public student(String name,int age){
    10. this.name=name;
    11. this.age=age;
    12. }
    13. //成员方法
    14. public void setName(String name){
    15. this.name=name;
    16. }
    17. public String getName(){
    18. return name;
    19. }
    20. public void setAge(int age){
    21. this.age=age;
    22. }
    23. public int getAge(){
    24. return age;
    25. }
    26. }

    测试类:

    1. package com.test;
    2. import java.util.HashMap;
    3. import java.util.Map;
    4. import java.util.Set;
    5. public class studentdemo {
    6. public static void main(String[] args) {
    7. //创建集合对象
    8. HashMap h=new HashMap();
    9. //创建学生对象
    10. student s1=new student("zyy",10);
    11. student s2=new student("zy",11);
    12. student s3=new student("z",12);
    13. //把学生添加到集合
    14. h.put("0124",s1);
    15. h.put("012",s2);
    16. h.put("01",s3);
    17. //遍历集合1:键找值
    18. Set k = h.keySet();
    19. for(String s: k){
    20. student v=h.get(s);
    21. System.out.println(s+","+v.getName()+","+v.getAge());
    22. }
    23. System.out.println("***********");
    24. //遍历集合2:键值对对象找键核值
    25. Set> e = h.entrySet();
    26. for(Map.Entry v:e){
    27. String key =v.getKey();
    28. student va=v.getValue();
    29. System.out.println(key+","+va.getName()+","+va.getAge());
    30. }
    31. }
    32. }

    案例:HashMap集合存储学生对象并遍历

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

    思路:
    定义学生类
    创建HashMap集合对象

    创建学生对象
    把学生添加到集合

    遍历集合
    在学生类中重写两个方法
            hashCode()

            equals()

    学生类:

    1. package com.test;
    2. public class student {
    3. //成员变量
    4. private String name;
    5. private int age;
    6. //构造方法
    7. public student(){
    8. }
    9. public student(String name,int age){
    10. this.name=name;
    11. this.age=age;
    12. }
    13. //成员方法
    14. public void setName(String name){
    15. this.name=name;
    16. }
    17. public String getName(){
    18. return name;
    19. }
    20. public void setAge(int age){
    21. this.age=age;
    22. }
    23. public int getAge(){
    24. return age;
    25. }
    26. @Override
    27. public boolean equals(Object o) {
    28. if (this == o) return true;
    29. if (o == null || getClass() != o.getClass()) return false;
    30. student student = (student) o;
    31. if (age != student.age) return false;
    32. return name != null ? name.equals(student.name) : student.name == null;
    33. }
    34. @Override
    35. public int hashCode() {
    36. int result = name != null ? name.hashCode() : 0;
    37. result = 31 * result + age;
    38. return result;
    39. }
    40. }

    测试类:

    1. package com.test;
    2. import java.util.HashMap;
    3. import java.util.Map;
    4. import java.util.Set;
    5. public class studentdemo {
    6. public static void main(String[] args) {
    7. //创建集合对象
    8. HashMap h=new HashMap();
    9. //创建学生对象
    10. student s1=new student("zyy",10);
    11. student s2=new student("zy",11);
    12. student s3=new student("z",12);
    13. student s4=new student("z",12);
    14. //把学生添加到集合
    15. h.put(s1,"洛阳");
    16. h.put(s2,"厦门");
    17. h.put(s3,"云南");
    18. h.put(s4,"新疆");
    19. //遍历集合1:键找值
    20. Set k = h.keySet();
    21. for(student s: k){
    22. String v=h.get(s);
    23. System.out.println(v +","+s.getName()+","+s.getAge());
    24. }
    25. System.out.println("***********");
    26. //遍历集合2:键值对对象找键核值
    27. Set> e = h.entrySet();
    28. for(Map.Entry v:e){
    29. student key =v.getKey();
    30. String va=v.getValue();
    31. System.out.println(va+","+key.getName()+","+key.getAge());
    32. }
    33. }
    34. }
  • 相关阅读:
    系统和系统实例-软件方法(下)第9章分析类图案例篇Part07
    小样本学习(2)--LibFewShot使用
    Canal 安装与入门
    Modelsim查看波形窗口内断言(SVA)消息指示器
    springboot 集成 apollo
    【技术精粹】AppGallery Connect开发精品实战课
    GitHub打不开解决方法——授人以渔
    开源MyBatisGenerator组件源码分析
    oracle数据库 表中有数据,通过plsql 工具 连接 查询全表,却查不到数据
    Spring核心系列——多yaml数据读取,@Value day1-1
  • 原文地址:https://blog.csdn.net/qq_62799214/article/details/126200487