• Java学习笔记(二十一)


    在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理Java知识点的第二十一篇博客。

    本篇博客介绍了Java的Map集合。

    本系列博客所有Java代码都使用IntelliJ IDEA编译运行,版本为2022.1。所用JDK版本为JDK11

    目录

    Map集合

    Map集合概述和特点

    Map集合的基本功能

    Map集合的获取功能

    Map集合的遍历

    键是String值是Student

    键是Student值是String

    集合嵌套ArrayList嵌套HashMap

    集合嵌套HashMap嵌套ArrayList

    统计字符串每个字符出现次数


    Map集合

    Map集合概述和特点

    Map集合的表示是Map,K是键的类型,V是值的类型。

    Map将键映射到值的对象,不能包含重复的键,每个键映射到最多一个值。

    创建Map集合的对象通过多态,具体的实现类是HashMap。使用HashMap需要导包,import java.util.HashMap

    在向HashMap加入元素时,如果键的值重复,则后加入的值为键对应的值。

    1. import java.util.Map;
    2. import java.util.HashMap;
    3. public class maptest1 {
    4. public static void main(String[] args){
    5. Map test = new HashMap();
    6. test.put("Rick","C2");
    7. test.put("Rose","TS");
    8. test.put("Rina","C3");
    9. test.put("Rina","TS");
    10. test.put("Rosa","C4");
    11. test.put("Pablo","C1");
    12. System.out.println(test);
    13. }
    14. }

    程序创建了一个HashMap,程序的输出是:

    {Pablo=C1, Rick=C2, Rose=TS, Rina=TS, Rosa=C4}

    Map集合的基本功能

    V put(K key,V value)向集合添加元素。

    V remove(Object key)根据键删除键值对元素。

    void clear()删除所有键值对。

    boolean containsKey(Object key)判断是否包含指定的键,包含为true,否则为false。

    boolean containsValue(Object value)判断集合是否包含指定的值,包含为true,否则为false。

    boolean isEmpty()判断集合是否为空,空为true,否则为false。

    int size()返回集合的长度,即元素个数。

    1. import java.util.Map;
    2. import java.util.HashMap;
    3. public class maptest2 {
    4. public static void main(String[] args){
    5. Map test = new HashMap();
    6. test.put("Matthew",145);
    7. test.put("Madeline",115);
    8. test.put("Maria",150);
    9. test.put("Max",75);
    10. test.put("Michael",145);
    11. test.put("Miriam",95);
    12. test.put("Marco",65);
    13. test.put("Marie",120);
    14. System.out.println(test);
    15. System.out.println(test.size());
    16. System.out.println(test.isEmpty());
    17. test.clear();
    18. System.out.println(test.isEmpty());
    19. test.put("Nicholas",65);
    20. test.put("Nora",75);
    21. test.put("Nicole",115);
    22. test.put("Narda",45);
    23. test.put("Nadine",45);
    24. test.put("Norman",130);
    25. System.out.println(test.size());
    26. System.out.println(test);
    27. test.remove("Narda");
    28. System.out.println(test.containsKey("Norman"));
    29. System.out.println(test.containsValue(140));
    30. System.out.println(test.size());
    31. System.out.println(test);
    32. }
    33. }

    程序的输出是:

    {Marco=65, Marie=120, Madeline=115, Max=75, Michael=145, Matthew=145, Maria=150, Miriam=95}
    8
    false
    true
    6
    {Nicole=115, Nadine=45, Norman=130, Narda=45, Nora=75, Nicholas=65}
    true
    false
    5
    {Nicole=115, Nadine=45, Norman=130, Nora=75, Nicholas=65}
     

    Map集合的获取功能

    V get(Object key)根据键获取值。

    Set keySet()获取所有键的集合。

    Collection values()获取所有值的集合。

    Set> entrySet()获取所有键值对对象的集合。

    1. import java.util.Collection;
    2. import java.util.Set;
    3. import java.util.Map;
    4. import java.util.HashMap;
    5. public class maptest3 {
    6. public static void main(String[] args){
    7. Map test = new HashMap();
    8. test.put("Grace",105);
    9. test.put("Guillermo",45);
    10. test.put("Gaston",105);
    11. test.put("Georgette",120);
    12. test.put("Gert",95);
    13. test.put("Greg",45);
    14. test.put("Gordon",50);
    15. test.put("Gabrielle",60);
    16. test.put("Genevieve",115);
    17. System.out.println(test.get("Georgette"));
    18. System.out.println(test.get("Grace"));
    19. Set key = test.keySet();
    20. for(String str:key){
    21. System.out.println(str);
    22. }
    23. Collection value = test.values();
    24. for(int i:value){
    25. System.out.print(i + " ");
    26. }
    27. }
    28. }

    程序用keySet方法获取键的集合,再用values方法获取值的集合。

    程序的输出是:

    120
    105
    Gordon
    Gabrielle
    Gert
    Genevieve
    Grace
    Guillermo
    Greg
    Georgette
    Gaston
    50   60   95   115   105   45   45   120   105   

    Map集合的遍历

    1. import java.util.Map;
    2. import java.util.HashMap;
    3. import java.util.Set;
    4. public class maplooktest1 {
    5. public static void main(String[] args){
    6. Map test = new HashMap();
    7. test.put("Odette",35);
    8. test.put("Olaf",90);
    9. test.put("Otto",100);
    10. test.put("Ophelia",100);
    11. test.put("Oscar",95);
    12. test.put("Olivia",115);
    13. Set keylist = test.keySet();
    14. for(String str:keylist){
    15. System.out.println(test.get(str));
    16. }
    17. }
    18. }

    程序的输出是:

    115
    100
    100
    90
    35
    95

    1. import java.util.Map;
    2. import java.util.HashMap;
    3. import java.util.Set;
    4. public class maplooktest2 {
    5. public static void main(String[] args){
    6. Map test = new HashMap();
    7. test.put("Odette",35);
    8. test.put("Olaf",90);
    9. test.put("Otto",100);
    10. test.put("Ophelia",100);
    11. test.put("Oscar",95);
    12. test.put("Olivia",115);
    13. Set> setlist = test.entrySet();
    14. for(Map.Entry kv:setlist){
    15. System.out.println("The name is " + kv.getKey());
    16. System.out.println("The number is " + kv.getValue());
    17. }
    18. }
    19. }

    程序用entrySet方法获得键值对的集合。对其中每个元素,可以使用getKey方法获得键,用getValue方法获得值。程序的输出是:

    The name is Olivia
    The number is 115
    The name is Ophelia
    The number is 100
    The name is Otto
    The number is 100
    The name is Olaf
    The number is 90
    The name is Odette
    The number is 35
    The name is Oscar
    The number is 95

    键是String值是Student

    这种嵌套的情况,先整体处理,再分开处理。

    1. public class studenthashmap1 {
    2. private String name;
    3. private int age;
    4. public studenthashmap1(){}
    5. public studenthashmap1(String name,int age){
    6. this.name = name;
    7. this.age = age;
    8. }
    9. public void setname(String name){
    10. this.name = name;
    11. }
    12. public String getname(){
    13. return name;
    14. }
    15. public void setage(int age){
    16. this.age = age;
    17. }
    18. public int getage(){
    19. return age;
    20. }
    21. }

    这是学生类。

    1. import java.util.Map;
    2. import java.util.HashMap;
    3. import java.util.Set;
    4. public class studenthashmaptest1 {
    5. public static void main(String[] args){
    6. Map test = new HashMap();
    7. studenthashmap1 stm1 = new studenthashmap1("Guillermo",25);
    8. studenthashmap1 stm2 = new studenthashmap1("Grace",19);
    9. studenthashmap1 stm3 = new studenthashmap1("Georgette",24);
    10. studenthashmap1 stm4 = new studenthashmap1("Gaston",18);
    11. studenthashmap1 stm5 = new studenthashmap1("Greg",23);
    12. studenthashmap1 stm6 = new studenthashmap1("Gert",23);
    13. test.put("1997EPAC",stm1);
    14. test.put("2021ATL",stm2);
    15. test.put("2016EPAC",stm3);
    16. test.put("2016ATL",stm4);
    17. test.put("1993EPAC",stm5);
    18. test.put("1999ATL",stm6);
    19. Set settest1 = test.keySet();
    20. for(String s:settest1){
    21. studenthashmap1 stm = test.get(s);
    22. System.out.print("The number is " + s);
    23. System.out.print(" and the name is " + stm.getname());
    24. System.out.println(" and the age is " + stm.getage());
    25. }
    26. Set> settest2 = test.entrySet();
    27. for(Map.Entry sstm:settest2){
    28. System.out.print("The number is " + sstm.getKey());
    29. System.out.print(" and the name is " + sstm.getValue().getname());
    30. System.out.println(" and the age is " + sstm.getValue().getage());
    31. }
    32. }
    33. }

    程序创建了一个HashMap,键为String,值为studenthashmap1类对象。程序使用两种方法遍历,一种是获取键的集合,然后遍历的过程中取出值。另一种是获取键值对,然后每次遍历就获取键和值。由于值是一个类,因此得到后还要再次操作。程序输出下面内容两次:

    The number is 1999ATL and the name is Gert and the age is 23
    The number is 2021ATL and the name is Grace and the age is 19
    The number is 2016ATL and the name is Gaston and the age is 18
    The number is 1997EPAC and the name is Guillermo and the age is 25
    The number is 2016EPAC and the name is Georgette and the age is 24
    The number is 1993EPAC and the name is Greg and the age is 23

    键是Student值是String

    1. public class studenthashmap2 {
    2. private String name;
    3. private int age;
    4. public studenthashmap2(){}
    5. public studenthashmap2(String name,int age){
    6. this.name = name;
    7. this.age = age;
    8. }
    9. public void setname(String name){
    10. this.name = name;
    11. }
    12. public String getname(){
    13. return name;
    14. }
    15. public void setage(int age){
    16. this.age = age;
    17. }
    18. public int getage(){
    19. return age;
    20. }
    21. }

    这是学生类。

    1. import java.util.Map;
    2. import java.util.HashMap;
    3. import java.util.Set;
    4. public class studenthashmaptest2 {
    5. public static void main(String[] args){
    6. Map test = new HashMap();
    7. studenthashmap2 shm1 = new studenthashmap2("Gilma",22);
    8. studenthashmap2 shm2 = new studenthashmap2("Gordon",16);
    9. studenthashmap2 shm3 = new studenthashmap2("Gil",21);
    10. studenthashmap2 shm4 = new studenthashmap2("Gabrielle",21);
    11. studenthashmap2 shm5 = new studenthashmap2("Genevieve",20);
    12. studenthashmap2 shm6 = new studenthashmap2("Gonzalo",14);
    13. test.put(shm1,"1994EPAC");
    14. test.put(shm2,"2006ATL");
    15. test.put(shm3,"2001ATL");
    16. test.put(shm4,"1989ATL");
    17. test.put(shm5,"2014EPAC");
    18. test.put(shm6,"2014ATL");
    19. Set settest1 = test.keySet();
    20. for(studenthashmap2 shm:settest1){
    21. System.out.print("The name is " + shm.getname());
    22. System.out.print(" and the age is " + shm.getage());
    23. System.out.println(" and the num is " + test.get(shm));
    24. }
    25. Set> settest2 = test.entrySet();
    26. for(Map.Entry shm:settest2){
    27. System.out.print("The name is " + shm.getKey().getname());
    28. System.out.print(" and the age is " + shm.getKey().getname());
    29. System.out.println(" and the num is " + shm.getValue());
    30. }
    31. }
    32. }

    程序创建了一个HashMap,键为studenthashmap2类对象,值为String。程序使用两种方法遍历,一种是获取键的集合,然后遍历的过程中取出值。另一种是获取键值对,然后每次遍历就获取键和值。由于键是一个类,因此得到后还要再次操作。程序输出下面内容两次:

    The name is Gordon and the age is 16 and the num is 2006ATL
    The name is Gabrielle and the age is 21 and the num is 1989ATL
    The name is Gilma and the age is 22 and the num is 1994EPAC
    The name is Gonzalo and the age is 14 and the num is 2014ATL
    The name is Gil and the age is 21 and the num is 2001ATL
    The name is Genevieve and the age is 20 and the num is 2014EPAC

    集合嵌套ArrayList嵌套HashMap

    1. import java.util.Map;
    2. import java.util.HashMap;
    3. import java.util.ArrayList;
    4. import java.util.Set;
    5. public class arraylisthashmap {
    6. public static void main(String[] args){
    7. ArrayList> arraytest = new ArrayList>();
    8. HashMap hm1 =new HashMap();
    9. hm1.put("Odette","2003ATL");
    10. hm1.put("Olaf","2015EPAC");
    11. HashMap hm2 = new HashMap();
    12. hm2.put("Otto","2016ATL");
    13. hm2.put("Orlene","1992EPAC");
    14. HashMap hm3 = new HashMap();
    15. hm3.put("Ophelia","2011ATL");
    16. hm3.put("Otis","2017EPAC");
    17. arraytest.add(hm1);
    18. arraytest.add(hm2);
    19. arraytest.add(hm3);
    20. for(HashMap hm:arraytest){
    21. Set settest = hm.keySet();
    22. for(String s:settest){
    23. System.out.print("The name is " + s);
    24. System.out.println(" and the age is " + hm.get(s));
    25. }
    26. }
    27. for(HashMap hm : arraytest){
    28. Set> settest = hm.entrySet();
    29. for(Map.Entry mess:settest) {
    30. System.out.print("The name is " + mess.getKey());
    31. System.out.println(" and the age is " + mess.getValue());
    32. }
    33. }
    34. }
    35. }

    ArrayList集合arraytest的成员是HashMap。因此遍历时先获取HashMap,然后按处理HashMap的方法获取键和值。程序输出下面内容两次:

    The name is Olaf and the age is 2015EPAC
    The name is Odette and the age is 2003ATL
    The name is Orlene and the age is 1992EPAC
    The name is Otto and the age is 2016ATL
    The name is Ophelia and the age is 2011ATL
    The name is Otis and the age is 2017EPAC

    集合嵌套HashMap嵌套ArrayList

    1. import java.util.Set;
    2. import java.util.Map;
    3. import java.util.HashMap;
    4. import java.util.ArrayList;
    5. public class hashmaparraylist {
    6. public static void main(String[] args){
    7. HashMap> maptest = new HashMap>();
    8. ArrayList al1 = new ArrayList();
    9. al1.add("Harvey");
    10. al1.add("Irma");
    11. al1.add("Maria");
    12. al1.add("Nate");
    13. maptest.put("2017ATLretire",al1);
    14. ArrayList al2 = new ArrayList();
    15. al2.add("Dennis");
    16. al2.add("Katrina");
    17. al2.add("Rita");
    18. al2.add("Stan");
    19. al2.add("Wilma");
    20. maptest.put("2005ATLretire",al2);
    21. ArrayList al3 = new ArrayList();
    22. al3.add("Luis");
    23. al3.add("Marilyn");
    24. al3.add("Opal");
    25. al3.add("Roxanne");
    26. maptest.put("1995ATLretire",al3);
    27. Set settest1 = maptest.keySet();
    28. for(String s:settest1){
    29. ArrayList arraytest1 = maptest.get(s);
    30. System.out.println(s + ":");
    31. int i;
    32. for(i = 0; i 1){
    33. System.out.println("The name is " + arraytest1.get(i));
    34. }
    35. }
    36. Set>> settest2 = maptest.entrySet();
    37. for(Map.Entry> me: settest2){
    38. System.out.println(me.getKey() + ":");
    39. ArrayList arraytest2 = me.getValue();
    40. int i;
    41. for(i = 0; i 1){
    42. System.out.println("The name is " + arraytest2.get(i));
    43. }
    44. }
    45. }
    46. }

    HashMap的键是String,值是ArrayList,因此先按处理HashMap的方法获取键和值。然后对ArrayList进行遍历。程序输出下面内容两次:

    2005ATLretire:
    The name is Dennis
    The name is Katrina
    The name is Rita
    The name is Stan
    The name is Wilma
    1995ATLretire:
    The name is Luis
    The name is Marilyn
    The name is Opal
    The name is Roxanne
    2017ATLretire:
    The name is Harvey
    The name is Irma
    The name is Maria
    The name is Nate

    统计字符串每个字符出现次数

    TreeMap是一种可以排序的Map,默认按键的升序排序。使用时需要导包,import java.util.TreeMap

    1. import java.util.Scanner;
    2. import java.util.TreeMap;
    3. import java.util.Set;
    4. public class stringchar {
    5. public static void main(String[] args){
    6. Scanner sc = new Scanner(System.in);
    7. String str = sc.nextLine();
    8. TreeMap tm = new TreeMap();
    9. int i;
    10. for(i = 0; i < str.length();i += 1){
    11. char ch = str.charAt(i);
    12. if(tm.get(ch) == null){
    13. tm.put(ch,1);
    14. }else{
    15. int temp = tm.get(ch);
    16. temp += 1;
    17. tm.put(ch,temp);
    18. }
    19. }
    20. StringBuilder sb = new StringBuilder();
    21. Set set = tm.keySet();
    22. for(char ch:set){
    23. sb.append(ch);
    24. sb.append('(');
    25. sb.append(tm.get(ch));
    26. sb.append(')');
    27. }
    28. String st = sb.toString();
    29. System.out.println(st);
    30. }
    31. }

    程序首先输入字符串,然后创建了一个TreeMap集合tm,然后遍历字符串,如果tm集合中没有值是这个字符的键,就将这个字符作为键,1作为值加入集合。否则,就取出键,将值加一作为值重新放入集合。然后构建字符串,格式是字符(次数)。

    程序的运行结果是:

    输入:NepartakMeranti
    输出:M(1)N(1)a(3)e(2)i(1)k(1)n(1)p(1)r(2)t(2)

  • 相关阅读:
    25张炫酷交互图表,一文入门Plotly
    浏览器上的快捷键
    OpenCV(四十三):Shi-Tomas角点检测
    二、uboot_源码分析
    微信小程序隐藏滚动条的方法
    代码随想录算法训练营第一天 | 704. 二分查找 | 27. 移除元素
    Android文件格式
    【springboot+vue】原生小程序电子班牌系统 智慧校园云平台源码
    Docker默认桥接网络是如何工作的
    操作系统八股文背诵版
  • 原文地址:https://blog.csdn.net/m0_71007572/article/details/126388343