我已經使用Google搜索,但沒有將HashMap<String, ArrayList<Class>>
轉換爲ArrayList<Class>
。請有人幫助我嗎?如何在Java中將HashMap <String,ArrayList <MyClass>>轉換爲ArrayList <MyClass>?
基本上,我想從調用構造函數改變我的方法getStudentLst,並將哈希映射轉換爲arrayList,如下所示。我已經嘗試了很多次,但是它一直在產生錯誤。我錯了嗎?
ArrayList<Student> arrStudLst = new ArrayList<Student>(hmpStudent.keySet());
Collectoins.sort(arrStudLst, new Comparator());
return arrStudLst;
,但它不工作,並生成錯誤「構造的ArrayList(設置)是未定義
任何幫助,非常感謝!阿丹
/* works well but i want to avoid calling constructor */
public ArrayList<Student> getStudentLst(HashMap<String, ArrayList<Student>> hmpStudent)
{
ArrayList<Student> arrStudLst = new ArrayList<Student>();
Set<String> keylst = hmpStudent.keySet();
Student student;
for(String keys: keylst)
{
for(Student f: hmpStudent.get(keys))
{
student = new Student(f.getName(),f.getGrade(), f.getTeacher()); arrStudLst.add(student);
}
}
Collections.sort(arrStudLst,new StudentComparator());
return arrStudLst;
}
你想獲得數組列表,鍵或值? – Suranga