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?

asked Apr 18, 2015 at 9:51

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);
}
answered Apr 18, 2015 at 9:53
3
  • I use Java 7 and there is no such method. Commented Apr 18, 2015 at 9:58
  • @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. Commented Apr 18, 2015 at 10:01
  • Thanks, but could you give me example code how to do it. Commented Apr 18, 2015 at 10:04
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);
}
answered Apr 18, 2015 at 10:05
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;
 }
}
answered Apr 18, 2015 at 10:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.