我猜你想存储每个年级所需的书籍。
enum Grades
{
SIXTH_GRADE, SEVENTH_GRADE;
}
public static void main(String[] args) throws IOException
{
Map<Grades, List<String>> literature = new HashMap<Grades, List<String>>();
literature.put(Grades.SIXTH_GRADE, new ArrayList<String>(Arrays.asList("Learning Ratio and Proportion Concepts and practice assessment","Number System")));
literature.put(Grades.SEVENTH_GRADE, new ArrayList<String>(Arrays.asList("Equations and expression","Geometry")));
// get books for 6th grade
List<String> books6th = literature.get(Grades.SIXTH_GRADE);
for (String book : books6th)
System.out.println(book);
// get books for 7th grade
List<String> books7th = literature.get(Grades.SEVENTH_GRADE);
Collection<List<String>> allBooks = literature.values(); // tname data
Set<Grades> allGrades = literature.keySet(); // data associated with gname
}
我什至会考虑与 ISBN 号、价格等成员一起创建一个class Book
。但这取决于您的用例。
我在这里使用了枚举,而不是字符串,所以地图不太容易出错。例如。如果有人不小心写"6th grade"
而不是"sixth grade"
,当然你也可以使用字符串常量。
使用枚举,您可以使用 anEnumMap
而不是HashMap
like
Map<Grades, List<String>> literature = new EnumMap<Grades, List<String>>(Grades.class);
.