PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 20, 2022

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

 September 20, 2022     android, hashmap, java, list     No comments   

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
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing