Java 如何转换HashMap<;字符串,ArrayList<;MyClass>&燃气轮机;到ArrayList<;MyClass>;在爪哇?
我在谷歌上搜索过,但没有将Java 如何转换HashMap<;字符串,ArrayList<;MyClass>&燃气轮机;到ArrayList<;MyClass>;在爪哇?,java,Java,我在谷歌上搜索过,但没有将HashMap转换成ArrayList。有人能帮我吗 基本上,我想将下面的getStudentLst方法从调用构造函数更改为将哈希映射转换为arrayList,如下所示。我试了很多次,但总是出错。我错在哪里 ArrayList<Student> arrStudLst = new ArrayList<Student>(hmpStudent.keySet()); Collectoins.sort(arrStudLst, new Compara
HashMap
转换成ArrayList
。有人能帮我吗
基本上,我想将下面的getStudentLst方法从调用构造函数更改为将哈希映射转换为arrayList,如下所示。我试了很多次,但总是出错。我错在哪里
ArrayList<Student> arrStudLst = new ArrayList<Student>(hmpStudent.keySet());
Collectoins.sort(arrStudLst, new Comparator());
return arrStudLst;
ArrayList arrStudLst=新的ArrayList(hmpStudent.keySet());
sort(arrStudLst,newcomparator());
返回arrlst;
但它不起作用并生成错误“构造函数ArrayList(Set)未定义”
非常感谢任何帮助!阿丹
/* 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;
}
/*运行良好,但我希望避免调用构造函数*/
公共阵列列表getStudentLst(HashMap hmpStudent)
{
ArrayList arrStudLst=新的ArrayList();
Set keylst=hmpStudent.keySet();
学生;
用于(字符串键:keylst)
{
用于(学生f:HMP学生获取(钥匙))
{
学生=新学生(f.getName()、f.getGrade()、f.getTeacher());arrStudLst.add(学生);
}
}
Collections.sort(arrStudLst,newstudentcomparator());
返回arrlst;
}
您试图用地图的键初始化列表。但键是字符串,它们不是学生
您需要返回一个包含地图每个值中每个学生的列表。因此代码为:
Set<Student> allStudents = new HashSet<Student>(); // use a set to avoid duplicate students
for (List<Student> list : map.values()) {
allStudents.addAll(list);
}
List<Student> allStudentsAsList = new ArrayList<Student>(allStudents);
Set allStudents=new HashSet();//使用集合避免重复的学生
对于(列表:map.values()){
allStudents.addAll(列表);
}
List allStudentsAsList=新数组列表(allStudents);
我可以试试
ArrayList<MyClass> list = new ArrayList<>();
for (ArrayList<MyClass> l : map.values()) {
list.addAll(l);
}
ArrayList list=new ArrayList();
for(ArrayList l:map.values()){
列表。添加全部(l);
}
键是一个字符串,所以
hmpStudent.keySet()
将返回一个列表
,而不是列表
newarraylist(hmpStudent.keySet());
因此,上述语句将失败。您可以使用下面提到的方法
public List<Student> getAllStudents(Map<String, ArrayList<Student> map){
List<Student> allStudents = new ArrayList<Student>();
for (List<Student> list : map.values()) {
allStudents.addAll(list);
}
return allStudents;
}
public List getAllStudents(Map如果Map中的ArrayList
很大,但Map中的元素较少,那么这就是您可以尝试的
public ArrayList<Student> getStudentLst(HashMap<String, ArrayList<Student>> hmpStudent)
{
List<Student> arrStudLst = new ArrayList<Student>();
for(ArrayList<Student> studentLst : hmpStudent.values()) {
arrStudLst.addAll(studentLst);
}
Collections.sort(arrStudLst,new StudentComparator());
return arrStudLst;
}
public ArrayList getStudentLst(HashMap hmpStudent)
{
List arrStudLst=new ArrayList();
对于(ArrayList studentLst:hmpStudent.values()){
arrStudLst.addAll(studentLst);
}
Collections.sort(arrStudLst,newstudentcomparator());
返回arrlst;
}
您想从数组列表、键或值中得到什么?为什么要创建这个列表的中间列表,而不是直接迭代通过map.values()返回的列表集合
?@JB Nizet:我错了。我脑子里还有别的想法,但现在意识到它实际上在做其他人发布的事情。我不确定删除我的答案是否是一个正确的选择。我现在更新了我的答案。我尝试了所有建议,都很好。非常感谢。
public ArrayList<Student> getStudentLst(HashMap<String, ArrayList<Student>> hmpStudent)
{
List<Student> arrStudLst = new ArrayList<Student>();
for(ArrayList<Student> studentLst : hmpStudent.values()) {
arrStudLst.addAll(studentLst);
}
Collections.sort(arrStudLst,new StudentComparator());
return arrStudLst;
}