user1926649
user1926649

Reputation: 35

How to convert HashMap<String, ArrayList<MyClass>> to ArrayList<MyClass> in java?

I have googled but did not come accross converting HashMap<String, ArrayList<Class>> to ArrayList<Class>. Could someone help me please?

Basically, i want to change my method getStudentLst below from calling constructor and convert hash map to arrayList as below. I have tried many times but it keeps generating error.Where i am going wrong?

  ArrayList<Student> arrStudLst = new ArrayList<Student>(hmpStudent.keySet());
  Collectoins.sort(arrStudLst, new Comparator());
  return arrStudLst;

but it did not work and generate error "The constructor ArrayList(Set) is undefined

any help much appreciated! adan

/* 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;
}

Upvotes: 0

Views: 2241

Answers (4)

Parvez
Parvez

Reputation: 641

If your ArrayList in map is big but fewer elements in map then this is what you can try.

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;
}

Upvotes: 0

Rahul
Rahul

Reputation: 16335

The key is a String, So hmpStudent.keySet() will return a List<String> and not a List<Student>

 new ArrayList<Student>(hmpStudent.keySet());

Hence the above statement will fail. You can use the below mentioned method

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;
}

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

I can try

ArrayList<MyClass> list = new ArrayList<>();
for (ArrayList<MyClass> l : map.values()) {
    list.addAll(l);
}

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691655

You tried to initialize the list with the keys of your map. But the keys are Strings, they're not Students.

You need to return a list containing every student in every value of the map. So the code would be:

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);

Upvotes: 1

Related Questions