如何做Hashmap我有像
这样的值列表{"tname":"Learning Ratio and Proportion Concepts and practice assessment","gname":"Sixth grade"},
{"tname":"Number System","gname":"Sixth grade"},
{"tname":"quations and expression","gname":"Seventh grade"},{"tname":"Geometry","gname":"Seventh grade"}
我想存储为
list<String>={"Sixth grade","Seventh grade"}
list(list<String>)={{"Learning Ratio and Proportion Concepts and practice assessment","Number System"},{"quations and expression","Geometry"}}
任何人都可以帮助我...
答案 0 :(得分:0)
将ArrayList与HashMap一起使用
HashMap<String,ArrayList<String[]>> map = new HashMap<String,ArrayList<String[]>>();
ArrayList<String[]> theList1 = new ArrayList()<String[]>();
theList1.add(new String[] {"Learning Ratio and Proportion Concepts and practice assessment","Sixth grade"});
map.put("tname", theList1);
答案 1 :(得分:0)
我猜你想存储每个年级所需的书籍。
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
。但这取决于您的使用案例。
我在这里使用了枚举,而不是字符串,所以地图不容易出错。 E. g。如果有人意外地写"6th grade"
而不是"sixth grade"
,当然你也可以使用字符串常量。
使用枚举,您可以使用EnumMap
代替HashMap
Map<Grades, List<String>> literature = new EnumMap<Grades, List<String>>(Grades.class);
。