Home > Enterprise >  How to create a HashMap that would have String as key and the value would be another HashMap from on
How to create a HashMap that would have String as key and the value would be another HashMap from on

Time:09-09

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.

CodePudding user response:

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

CodePudding user response:

I am answering my own question as I solved it if anyone has a better answer please passed your answer aswell, ill accept another answer suitable and efficient answer.

            for( int i = 0; i<assignmentsdataArrayList.size();i   ){
                if (hashMap.size()>0){
                    if (hashMap.get(assignmentsdataArrayList.get(i).getGroupName())==null){
                        hashMap.put(assignmentsdataArrayList.get(i).getGroupName(),new ArrayList<Assignmentsdata>());
                        hashMap.get(assignmentsdataArrayList.get(i).getGroupName()).add(assignmentsdataArrayList.get(i));
                    }else{
                        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));
                }
            }

// above part is already in the question. the second part i looped through the hashMap then the list once again, and checking if list and map entry have same group name, then made the startdate key that indexed element from the list.

            HashMap<String, Map.Entry<String, ArrayList<Assignmentsdata>>> hashMapHashMap = new HashMap<>();
            for (var entry : hashMap.entrySet()){
                for( int j = 0; j<assignmentsdataArrayList.size();j   ){
                     if (assignmentsdataArrayList.get(j).getGroupName()==entry.getKey()){
                        hashMapHashMap.put(assignmentsdataArrayList.get(j).getStartTime(),entry);
                    }
                }
               hashMapHashMap.put(entry.getValue().get())

            }
  • Related