3

I have class named Student and it has String attribute date. I have list of all my Students and now I want to create multiple ArrayLists which are grouped by their dates.

I want to use hashmap:

ArrayList students = getStudents();

Map map<String, ArrayList<Student>> = new HashMap<String, ArrayList<Student>);

for (Student i: students) {
 // There must be something
}

How I can create multiple ArrayLists of Students which are grouped by their String value of their attribute?

3 Answers 3

5

The easiest way it to use Java 8 Streams :

Map<String, List<Student>> map =
    students.stream()
            .collect(Collectors.groupingBy(Student::getDate));

Where getDate is the method of Student class by which you wish to group the Students.

To complete the answer for pre-Java 8 code:

Map<String, List<Student>> map = new HashMap<>();
for (Student s : students) {
    List<Student> list = map.get(s.getDate());
    if (list == null) {
        list = new ArrayList<Student>();
        map.put (s.getDate(), list);
    }
    list.add (s);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I use Java 7 and there is no such method.
@zxcdsa980 If you can't use Java 8, you'll have to do it the hard way (well, not that hard actually) - iterate over all the students. For each student, check if map.containsKey(student.getDate()) for that student. If yes, add that student to the ArrayList of that date. If not, create a new ArrayList, add the student to it and put it in the map with the date as key.
Thanks, but could you give me example code how to do it.
3

Try this:

Map<String, List<Student>> map= new HashMap<String, List<Student>();
List<Student> list;
for (Student student: students) {
  list = map.get(student.getDate());
  if (list == null) {
      list = new ArrayList<Student>();
      map.put(student.getDate(), list);
  } 
  list.add(student);
}

Comments

0

The idea is to check if the date already exists in the map. If it exists add the student to the corresponding list. If it does not, add a new list with the student as the 1st record in the new list.

Here is what works.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

    public class Main {

        public static void main(String[] args) {
            ArrayList<Student> students = getStudents();

            Map<String, ArrayList<Student>> map = new HashMap<String,      ArrayList<Student>>();

            for (Student i : students) {
               if (map.containsKey(i.getDate())) {
                  map.get(i.getDate()).add(i);
               } else {
                  ArrayList<Student> newList = new ArrayList<Student>();
                  newList.add(i);
                  map.put(i.getDate(), newList);
               }
            }

            System.out.println(map);
        }

    private static ArrayList<Student> getStudents() {
        ArrayList<Student> list = new ArrayList<Student>();
        list.add(new Student("Hari", "12/05/2015"));
        list.add(new Student("Zxc", "14/05/2015"));
        list.add(new Student("Bob", "12/05/2015"));
        list.add(new Student("Ram", "14/05/2015"));

        return list;
    }

}

class Student {
    public Student(String name, String date) {
        this.name = name;
        this.date = date;
    }

    private String name;
    private String date;

    public String getDate() {
        return date;
    }

    @Override
    public String toString() {
        return name;
    }

}

Comments

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.