rikao7/src/main/java/org/example/StudentApplication.java

49 lines
1.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package org.example;
import org.example.domain.Student;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
public class StudentApplication {
public static void main(String[] args) {
// 设置编码(仅在需要时启用)
try {
System.setOut(new java.io.PrintStream(System.out,true,"UTF-8"));
} catch (java.io.UnsupportedEncodingException e) {
System.err.println("无法设置UTF-8编码" + e.getMessage());
}
//创建HashMap对象
HashMap<Integer, Student> studentHashMap = new HashMap<>();
//创建两个学生对象
Student studentOne = new Student(1, "梦钰", 22, "男", new BigDecimal(100));
Student studentTwo = new Student(2, "钰萍", 18, "女", new BigDecimal(100));
//保存到HashMap集合中键为学生编号值为学生对象
studentHashMap.put(studentOne.getStudentId(),studentOne);
studentHashMap.put(studentTwo.getStudentId(),studentTwo);
System.out.println("第一种entrySet遍历方法:");
for (Map.Entry<Integer, Student> studentEntry : studentHashMap.entrySet()) {
System.out.println("key" + studentEntry.getKey() + " value" + studentEntry.getValue());
}
System.out.println("\n第二种keySet遍历方法:");
for (Integer student : studentHashMap.keySet()) {
System.out.println("key" + student + " value" + studentHashMap.get(student));
}
System.out.println("\n第三种values遍历方法:");
for (Student student : studentHashMap.values()) {
System.out.println( " value" + student);
}
}
}