0

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;
}
1
  • What do you want to get to array list , keys or values ? Commented Dec 24, 2012 at 13:28

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried all suggestions and all worked well. Thank you so much.
0

I can try

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

Comments

0

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

Comments

0

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

2 Comments

Why create this intermediate list of lists, instead of iterating directly through the collection of lists returned by map.values()?
@JB Nizet: My mistake. I had something else in my mind but realized now that it is effectively doing the same thing that others have posted. I am not sure if deleting my answer is a right choice. Updated my answer now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.