要循环输出 HashMap 中的键值对,你可以使用 Java 中的迭代器或者使用 Java 8+ 提供的新特性如 forEach 方法。
增强型for循环和forEach语句不是一回事!
可以认为增强型for循环是while循环的改善版本
学习测试代码
package com.nuc;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Time : 2023/9/12 11:27
* Author : 王摇摆
* FileName: Student.java
* Software: IntelliJ IDEA 2020.2.2
* Blog :https://blog.csdn.net/weixin_44943389?type=blog
*/
public class Student {
private String id;
private String name;
private static final HashMap<String, Student> cache = new HashMap<>();
public Student(String id, String name) {
this.id = id;
this.name = name;
}
public static void createStudent(String inputId, String inputName) {
Student student = cache.get(inputId);
if (student == null) {
//新建学生对象,并把他放到学生map中
Student stu = new Student(inputId, inputName);
cache.put(inputId, stu);
System.out.println("Created the Student:" + stu.toString());
} else
//从缓存中直接拿出数据
System.out.println("Get the student from Cache:" + student.toString());
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}' + "@" + Integer.toHexString(this.hashCode());
}
//使用forEach语句将hashmap中的数据迭代输出
public static void print1() {
cache.forEach((key, value) -> System.out.println(key + " " + value));
}
//使用iterator迭代器将map中的数据输出
public static void print2() {
Iterator<Map.Entry<String, Student>> iterator = cache.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Student> entry = iterator.next();
String key = entry.getKey();
Student value = entry.getValue();
System.out.println(key + ":" + value);
}
}
public static void print3() {
for (Map.Entry<String, Student> entry : cache.entrySet()) {
String key = entry.getKey();
Student value = entry.getValue();
System.out.println(key + ":" + value);
}
}
}
程序主类
/**
* Time : 2023/9/12 11:10
* Author : 王摇摆
* FileName: Main.java
* Software: IntelliJ IDEA 2020.2.2
* Blog :https://blog.csdn.net/weixin_44943389?type=blog
*/
package com.nuc;
public class Main {
public static void main(String[] args) {
// test1();
test2();
}
private static void test1() {
Integer num1 = Integer.valueOf("123");
Integer num2 = Integer.valueOf("123");
System.out.println(num1.toString());
System.out.println(num2.toString());
System.out.println(num1 == num2);
System.out.println(num1.equals(num2));
}
private static void test2() {
//开始对学生类的hashmap进行测试
Student.createStudent("1", "王国炜1");
Student.createStudent("2", "王国炜2");
Student.createStudent("3", "王国炜3");
Student.createStudent("3", "王国炜3");
System.out.println("\n===========Print the HashMap================");
Student.print1();
Student.print2();
Student.print3();
}
}
运行结果
"C:\Program Files\Java\jdk1.8.0_60\bin\java.exe" "-javaagent:D:\IntelliJ IDEA Community Edition 2020.2.2\lib\idea_rt.jar=64470:D:\IntelliJ IDEA Community Edition 2020.2.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_60\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_60\jre\lib\rt.jar;C:\Users\Administrator\Desktop\Code\Design_Mode\liaoxuefeng\out\production\享元模式" com.nuc.Main
Created the Student:Student{id='1', name='王国炜1'}@1b6d3586
Created the Student:Student{id='2', name='王国炜2'}@4554617c
Created the Student:Student{id='3', name='王国炜3'}@74a14482
Get the student from Cache:Student{id='3', name='王国炜3'}@74a14482
===========Print the HashMap================
1 Student{id='1', name='王国炜1'}@1b6d3586
2 Student{id='2', name='王国炜2'}@4554617c
3 Student{id='3', name='王国炜3'}@74a14482
1:Student{id='1', name='王国炜1'}@1b6d3586
2:Student{id='2', name='王国炜2'}@4554617c
3:Student{id='3', name='王国炜3'}@74a14482
1:Student{id='1', name='王国炜1'}@1b6d3586
2:Student{id='2', name='王国炜2'}@4554617c
3:Student{id='3', name='王国炜3'}@74a14482
Process finished with exit code 0