在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理Java知识点的第二十一篇博客。
本篇博客介绍了Java的Map集合。
本系列博客所有Java代码都使用IntelliJ IDEA编译运行,版本为2022.1。所用JDK版本为JDK11。
目录
Map集合的表示是Map
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
test = new HashMap(); - 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}
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
test = new HashMap(); - 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}
V get(Object key)根据键获取值。
Set
Collection
Set
- 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
test = new HashMap(); - 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
key = test.keySet(); - for(String str:key){
- System.out.println(str);
- }
-
- Collection
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
- import java.util.Map;
- import java.util.HashMap;
- import java.util.Set;
- public class maplooktest1 {
- public static void main(String[] args){
- Map
test = new HashMap(); - 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
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
test = new HashMap(); - 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
> setlist = test.entrySet(); - for(Map.Entry
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
这种嵌套的情况,先整体处理,再分开处理。
- 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
test = new HashMap(); - 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
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
> settest2 = test.entrySet(); - for(Map.Entry
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
- 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
test = new HashMap(); - 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
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
> settest2 = test.entrySet(); - for(Map.Entry
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
- 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
> arraytest = new ArrayList>(); - HashMap
hm1 =new HashMap(); - hm1.put("Odette","2003ATL");
- hm1.put("Olaf","2015EPAC");
- HashMap
hm2 = new HashMap(); - hm2.put("Otto","2016ATL");
- hm2.put("Orlene","1992EPAC");
-
- HashMap
hm3 = new HashMap(); - hm3.put("Ophelia","2011ATL");
- hm3.put("Otis","2017EPAC");
- arraytest.add(hm1);
- arraytest.add(hm2);
- arraytest.add(hm3);
-
- for(HashMap
hm:arraytest){ - Set
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
hm : arraytest){ - Set
> settest = hm.entrySet(); - for(Map.Entry
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
- 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
> maptest = new HashMap>(); -
- ArrayList
al1 = new ArrayList(); - al1.add("Harvey");
- al1.add("Irma");
- al1.add("Maria");
- al1.add("Nate");
- maptest.put("2017ATLretire",al1);
-
- ArrayList
al2 = new ArrayList(); - al2.add("Dennis");
- al2.add("Katrina");
- al2.add("Rita");
- al2.add("Stan");
- al2.add("Wilma");
- maptest.put("2005ATLretire",al2);
-
- ArrayList
al3 = new ArrayList(); - al3.add("Luis");
- al3.add("Marilyn");
- al3.add("Opal");
- al3.add("Roxanne");
- maptest.put("1995ATLretire",al3);
- Set
settest1 = maptest.keySet(); - for(String s:settest1){
- ArrayList
arraytest1 = maptest.get(s); - System.out.println(s + ":");
- int i;
- for(i = 0; i
1){ - System.out.println("The name is " + arraytest1.get(i));
- }
- }
-
- Set
>> settest2 = maptest.entrySet(); - for(Map.Entry
> me: settest2){ - System.out.println(me.getKey() + ":");
- ArrayList
arraytest2 = me.getValue(); - int i;
- for(i = 0; 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
tm = new TreeMap(); - 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
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)