简体   繁体   中英

Create multiple Array lists by using Hashmap

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?

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 Student s.

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

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM