How to create a HashMap that would have String as key and the value would be another HashMap from one list of custom object?

Issue

I have a list of custom object,

public class Assignmentsdata {

    String assignmentId;
    String teacherId;
    String groupName;
    String sectionId;
    String levelId;
    String startTime;
}

ArrayList<Assignmentsdata> list = new ArrayList<>();

lets say there are 20 elements in that list.

Now I want to get the output which is a hashmap of startTime as a key and the Value would be a new HashMap of GroupID and a list of Assignments of those that had the same groupName.

OutPut Example

HashMap<startTime,HasMap<groupName,List> hashMap = new HashMap();

a little more insight about the problem: First I want to categorise based on startTime(Month) then i want to categorise based on groupName, Thanks in advance.

I have successfully categorised based on group name and created a map through below code:

 for( int i = 0; i<assignmentsdataArrayList.size();i++ ){
                if (hashMap.size()>0){
                    hashMap.get(assignmentsdataArrayList.get(i).getGroupName()).add(assignmentsdataArrayList.get(i));
                }else {
                    hashMap.put(assignmentsdataArrayList.get(i).getGroupName(),new ArrayList<Assignmentsdata>());
                    hashMap.get(assignmentsdataArrayList.get(i).getGroupName()).add(assignmentsdataArrayList.get(i));
                }
            }

After that I am lost on how to categorise this hashmap based on the startDate and create a hashmap that would look like the above hashmap in the output heading.

Solution

your code may throw a NullPointerException at the first if branch

 if (hashMap.size()>0)
{hashMap.get(assignmentsdataArrayList.get(i).getGroupName()).add(assignmentsdataArrayList.get(i));
 }

the map.size()>0 doesnt means the Value of GroupName has put a new ArrayList already.

the anwser of using loop should like this

   Map<String, Map<String, List<Assignmentsdata>>> map = new HashMap<>();
        for (Assignmentsdata assignmentsdata : list) {
            if (!map.containsKey(assignmentsdata.getStartTime())) {
                map.put(assignmentsdata.getStartTime(), new HashMap<>());
            }
            Map<String, List<Assignmentsdata>> startTimeMap = map.get(assignmentsdata.startTime);
            if (!startTimeMap.containsKey(assignmentsdata.getGroupName())) {
                startTimeMap.put(assignmentsdata.getGroupName(), new ArrayList<>());
            }
            startTimeMap.get(assignmentsdata.groupName).add(assignmentsdata);
        }

or you could use the java stream().collect(Collectors.groupingBy()) api to get the result easily

Map<String, Map<String, List<Assignmentsdata>>> result = list.stream()
            .collect(Collectors.groupingBy(Assignmentsdata::getStartTime,Collectors.groupingBy(Assignmentsdata::getGroupName)));

Answered By – Marzen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published