Java中HashMap、集合实现及相关概念详解
1. HashMap的遍历
在Java编程中,我们常常需要遍历哈希表(HashMap)的内容,可能只遍历键、只遍历值,或者同时遍历键和值。下面以 ReportCard
类为例进行说明。
package sis.report;
import java.util.*;
import sis.studentinfo.*;
public class ReportCard {
static final String A_MESSAGE = "Excellent";
static final String B_MESSAGE = "Very good";
static final String C_MESSAGE = "Hmmm...";
static final String D_MESSAGE = "You're not trying";
static final String F_MESSAGE = "Loser";
private Map<Student.Grade, String> messages = null;
public String getMessage(Student.Grade grade) {
return getMessages().get(grade);
}
private Map<Student.Grade, String> getMessages() {
if (messages == null)
loadMessages(