在完成对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>,K是键的类型,V是值的类型。

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

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

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

import java.util.Map;
import java.util.HashMap;
public class maptest1 {public static void main(String[] args){Map<String,String> test = new HashMap<String,String>();test.put("Rick","C2");test.put("Rose","TS");test.put("Rina","C3");test.put("Rina","TS");test.put("Rosa","C4");test.put("Pablo","C1");System.out.println(test);}
}

程序创建了一个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()返回集合的长度,即元素个数。

import java.util.Map;
import java.util.HashMap;
public class maptest2 {public static void main(String[] args){Map<String,Integer> test = new HashMap<String,Integer>();test.put("Matthew",145);test.put("Madeline",115);test.put("Maria",150);test.put("Max",75);test.put("Michael",145);test.put("Miriam",95);test.put("Marco",65);test.put("Marie",120);System.out.println(test);System.out.println(test.size());System.out.println(test.isEmpty());test.clear();System.out.println(test.isEmpty());test.put("Nicholas",65);test.put("Nora",75);test.put("Nicole",115);test.put("Narda",45);test.put("Nadine",45);test.put("Norman",130);System.out.println(test.size());System.out.println(test);test.remove("Narda");System.out.println(test.containsKey("Norman"));System.out.println(test.containsValue(140));System.out.println(test.size());System.out.println(test);}
}

程序的输出是:

{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<K> keySet()获取所有键的集合。

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

Set<Map.Entry<K,V>> entrySet()获取所有键值对对象的集合。

import java.util.Collection;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
public class maptest3 {public static void main(String[] args){Map<String,Integer> test = new HashMap<String,Integer>();test.put("Grace",105);test.put("Guillermo",45);test.put("Gaston",105);test.put("Georgette",120);test.put("Gert",95);test.put("Greg",45);test.put("Gordon",50);test.put("Gabrielle",60);test.put("Genevieve",115);System.out.println(test.get("Georgette"));System.out.println(test.get("Grace"));Set<String> key = test.keySet();for(String str:key){System.out.println(str);}Collection<Integer> value = test.values();for(int i:value){System.out.print(i + "   ");}}
}

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

程序的输出是:

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

Map集合的遍历

import java.util.Map;
import java.util.HashMap;
import java.util.Set;
public class maplooktest1 {public static void main(String[] args){Map<String,Integer> test = new HashMap<String,Integer>();test.put("Odette",35);test.put("Olaf",90);test.put("Otto",100);test.put("Ophelia",100);test.put("Oscar",95);test.put("Olivia",115);Set<String> keylist = test.keySet();for(String str:keylist){System.out.println(test.get(str));}}
}

程序的输出是:

115
100
100
90
35
95

import java.util.Map;
import java.util.HashMap;
import java.util.Set;
public class maplooktest2 {public static void main(String[] args){Map<String,Integer> test = new HashMap<String,Integer>();test.put("Odette",35);test.put("Olaf",90);test.put("Otto",100);test.put("Ophelia",100);test.put("Oscar",95);test.put("Olivia",115);Set<Map.Entry<String,Integer>> setlist = test.entrySet();for(Map.Entry<String,Integer> kv:setlist){System.out.println("The name is " + kv.getKey());System.out.println("The number is " + kv.getValue());}}
}

程序用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

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

public class studenthashmap1 {private String name;private int age;public studenthashmap1(){}public studenthashmap1(String name,int age){this.name = name;this.age = age;}public void setname(String name){this.name = name;}public String getname(){return name;}public void setage(int age){this.age = age;}public int getage(){return age;}
}

这是学生类。

import java.util.Map;
import java.util.HashMap;
import java.util.Set;
public class studenthashmaptest1 {public static void main(String[] args){Map<String,studenthashmap1> test = new HashMap<String,studenthashmap1>();studenthashmap1 stm1 = new studenthashmap1("Guillermo",25);studenthashmap1 stm2 = new studenthashmap1("Grace",19);studenthashmap1 stm3 = new studenthashmap1("Georgette",24);studenthashmap1 stm4 = new studenthashmap1("Gaston",18);studenthashmap1 stm5 = new studenthashmap1("Greg",23);studenthashmap1 stm6 = new studenthashmap1("Gert",23);test.put("1997EPAC",stm1);test.put("2021ATL",stm2);test.put("2016EPAC",stm3);test.put("2016ATL",stm4);test.put("1993EPAC",stm5);test.put("1999ATL",stm6);Set<String> settest1 = test.keySet();for(String s:settest1){studenthashmap1 stm = test.get(s);System.out.print("The number is " + s);System.out.print(" and the name is " + stm.getname());System.out.println(" and the age is " + stm.getage());}Set<Map.Entry<String,studenthashmap1>> settest2 = test.entrySet();for(Map.Entry<String,studenthashmap1> sstm:settest2){System.out.print("The number is " + sstm.getKey());System.out.print(" and the name is " + sstm.getValue().getname());System.out.println(" and the age is " + sstm.getValue().getage());}}
}

程序创建了一个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

public class studenthashmap2 {private String name;private int age;public studenthashmap2(){}public studenthashmap2(String name,int age){this.name = name;this.age = age;}public void setname(String name){this.name = name;}public String getname(){return name;}public void setage(int age){this.age = age;}public int getage(){return age;}
}

这是学生类。

import java.util.Map;
import java.util.HashMap;
import java.util.Set;
public class studenthashmaptest2 {public static void main(String[] args){Map<studenthashmap2,String> test = new HashMap<studenthashmap2,String>();studenthashmap2 shm1 = new studenthashmap2("Gilma",22);studenthashmap2 shm2 = new studenthashmap2("Gordon",16);studenthashmap2 shm3 = new studenthashmap2("Gil",21);studenthashmap2 shm4 = new studenthashmap2("Gabrielle",21);studenthashmap2 shm5 = new studenthashmap2("Genevieve",20);studenthashmap2 shm6 = new studenthashmap2("Gonzalo",14);test.put(shm1,"1994EPAC");test.put(shm2,"2006ATL");test.put(shm3,"2001ATL");test.put(shm4,"1989ATL");test.put(shm5,"2014EPAC");test.put(shm6,"2014ATL");Set<studenthashmap2> settest1 = test.keySet();for(studenthashmap2 shm:settest1){System.out.print("The name is " + shm.getname());System.out.print(" and the age is " + shm.getage());System.out.println(" and the num is " + test.get(shm));}Set<Map.Entry<studenthashmap2,String>> settest2 = test.entrySet();for(Map.Entry<studenthashmap2,String> shm:settest2){System.out.print("The name is " + shm.getKey().getname());System.out.print(" and the age is " + shm.getKey().getname());System.out.println(" and the num is " + shm.getValue());}}
}

程序创建了一个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

import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Set;
public class arraylisthashmap {public static void main(String[] args){ArrayList<HashMap<String,String>> arraytest = new ArrayList<HashMap<String,String>>();HashMap<String,String> hm1 =new HashMap<String,String>();hm1.put("Odette","2003ATL");hm1.put("Olaf","2015EPAC");HashMap<String,String> hm2 = new HashMap<String,String>();hm2.put("Otto","2016ATL");hm2.put("Orlene","1992EPAC");HashMap<String,String> hm3 = new HashMap<String,String>();hm3.put("Ophelia","2011ATL");hm3.put("Otis","2017EPAC");arraytest.add(hm1);arraytest.add(hm2);arraytest.add(hm3);for(HashMap<String,String> hm:arraytest){Set<String> settest = hm.keySet();for(String s:settest){System.out.print("The name is " + s);System.out.println(" and the age is " + hm.get(s));}}for(HashMap<String,String> hm : arraytest){Set<Map.Entry<String,String>> settest = hm.entrySet();for(Map.Entry<String,String> mess:settest) {System.out.print("The name is " + mess.getKey());System.out.println(" and the age is " + mess.getValue());}}}
}

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

import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
public class hashmaparraylist {public static void main(String[] args){HashMap<String,ArrayList<String>> maptest = new HashMap<String,ArrayList<String>>();ArrayList<String> al1 = new ArrayList<String>();al1.add("Harvey");al1.add("Irma");al1.add("Maria");al1.add("Nate");maptest.put("2017ATLretire",al1);ArrayList<String> al2 = new ArrayList<String>();al2.add("Dennis");al2.add("Katrina");al2.add("Rita");al2.add("Stan");al2.add("Wilma");maptest.put("2005ATLretire",al2);ArrayList<String> al3 = new ArrayList<String>();al3.add("Luis");al3.add("Marilyn");al3.add("Opal");al3.add("Roxanne");maptest.put("1995ATLretire",al3);Set<String> settest1 = maptest.keySet();for(String s:settest1){ArrayList<String> arraytest1 = maptest.get(s);System.out.println(s + ":");int i;for(i = 0; i <arraytest1.size(); i += 1){System.out.println("The name is " + arraytest1.get(i));}}Set<Map.Entry<String,ArrayList<String>>> settest2 = maptest.entrySet();for(Map.Entry<String,ArrayList<String>> me: settest2){System.out.println(me.getKey() + ":");ArrayList<String> arraytest2 = me.getValue();int i;for(i = 0; i <arraytest2.size(); i += 1){System.out.println("The name is " + arraytest2.get(i));}}}
}

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

import java.util.Scanner;
import java.util.TreeMap;
import java.util.Set;
public class stringchar {public static void main(String[] args){Scanner sc = new Scanner(System.in);String str = sc.nextLine();TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();int i;for(i = 0; i < str.length();i += 1){char ch = str.charAt(i);if(tm.get(ch) == null){tm.put(ch,1);}else{int temp = tm.get(ch);temp += 1;tm.put(ch,temp);}}StringBuilder sb = new StringBuilder();Set<Character> set = tm.keySet();for(char ch:set){sb.append(ch);sb.append('(');sb.append(tm.get(ch));sb.append(')');}String st = sb.toString();System.out.println(st);}
}

程序首先输入字符串,然后创建了一个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)

Java学习笔记(二十一)相关推荐

  1. Java学习笔记二:数据类型

    Java学习笔记二:数据类型 1. 整型:没有小数部分,允许为负数,Java整型分4种:int short long byte 1.1 Int最为常用,一个Int类型变量在内存中占用4个字节,取值范围 ...

  2. python3.4学习笔记(二十一) python实现指定字符串补全空格、前面填充0的方法

    python3.4学习笔记(二十一) python实现指定字符串补全空格.前面填充0的方法 Python zfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0. zfill()方法语法: ...

  3. Mr.J-- jQuery学习笔记(二十一)--模拟微博页面

    先看之前的节点操作方法:Mr.J-- jQuery学习笔记(二十)--节点操作方法 Mr.J-- jQuery学习笔记(五)--属性及属性节点 Mr.J-- jQuery学习笔记(十一)--事件委托  ...

  4. kvm虚拟化学习笔记(二十一)之KVM性能优化学习笔记

    本学习笔记系列都是采用CentOS6.x操作系统,KVM虚拟机的管理也是采用virsh方式,网上的很多的文章都基于ubuntu高版本内核下,KVM的一些新的特性支持更好,本文只是记录了CentOS6. ...

  5. java自定义一个timeout,Timeout操作符 RxJava 学习笔记二十一

    timeout用于检测在给定时间内observables没有及时响应.如果指定的时间量没有发出任何项目,则超时会使observables失败并出现TimeoutException. 我们将从debou ...

  6. Java学习笔记二十五:Java面向对象的三大特性之多态

    Java面向对象的三大特性之多态 一:什么是多态: 多态是同一个行为具有多个不同表现形式或形态的能力. 多态就是同一个接口,使用不同的实例而执行不同操作. 多态性是对象多种表现形式的体现. 现实中,比 ...

  7. Java学习笔记二十:Java中的内部类

    Java中的内部类 一:什么是内部类: (1).什么是内部类呢? 内部类( Inner Class )就是定义在另外一个类里面的类.与之对应,包含内部类的类被称为外部类. (2).那为什么要将一个类定 ...

  8. java学习笔记(二) ----基本数据类型应用

    &和&&和区别,&&如果等式一边不成立就短路,&不管&左边的成不成立,右边等式都执行 &&短路与, ||短路或 | 或,表达式两 ...

  9. Java学习笔记二十二:Java的方法重写

    Java的方法重写 一:什么是方法的重写: 如果子类对继承父类的方法不满意,是可以重写父类继承的方法的,当调用方法时会优先调用子类的方法. 语法规则 返回值类型.方法名.参数类型及个数都要与父类继承的 ...

  10. java actionscript_ActionScript(对比Java)学习笔记二

    27.ActionScript中的组件对象: 类似于Java中的Swing组件对象,ActionScript提供了很多组件!! (可以自行查看官网提供的组件列表学习,也可擦看已经拥有的flex+3+c ...

最新文章

  1. php实现stripos,php stripos()函数
  2. jquery.desktop.js 代码分析
  3. BGP路由反射器和防环机制
  4. FastDFS文件上传和下载流程
  5. kali 安装volatility_kali对Windows内存在线取证
  6. 单双目相机畸变校正--极线校正
  7. 对抗恶意程序的反虚拟化,百度安全提最新检测技术,具备三大特性
  8. Python基础——全局变量与局部变量
  9. 10以内逆向运算题_加减法启蒙系列 | 实战篇二(10以内减法)
  10. 利用图片指纹检测高相似度图片--相似图片搜索的原理
  11. 20X05 FCPX插件磨皮润肤美容插件 beautybox 4.2.3
  12. 2015计算机二级office真题,2015年计算机二级office题库及答案
  13. Origin 2021 创建双y轴
  14. STATA长面板数据分析
  15. 彻底解决联想手机数据连接不能上网问题(无需恢复出厂设置) 本文来自移动叔叔论坛 ,详细出处请参考:http://bbs.ydss.cn/thread-201115-1-1.html
  16. 人工智能行业每日必读(2020年1月14日)
  17. java数字音频最强教程之音频的王者之路(音频发烧友篇)
  18. 每次启动电脑,基本都出现这个错误,很长时间的启动过程。
  19. 学会给你的笔记本电池换“芯”
  20. linux读内存的命令devmem,嵌入式Linux调试_命令devmem_直接读写内存

热门文章

  1. nuxt 使用vuex在模块中无法调用全局的store
  2. 这份日志格式规范超棒的,拿走不谢(Java版)
  3. 关于UrlEncoder和UrlDecode
  4. spark 无法写入hive表Can not create the managed table,该如何解决?
  5. 数据开放,对于货运行业来说有着怎样的现实意义?
  6. 题解 CF650D 【Zip-line】
  7. Android 蓝牙开发(三) -- 低功耗蓝牙开发
  8. ICESat-1/GLAS14卫星数据处理1(附代码)-冰川物质平衡监测
  9. worker里的ajax,Web Worker 调用Ajax
  10. 直播2w人在线服务器,直播平台在线人数功能