1

I am using nested hash-map with ArrayList. My purpose is that, for examples,

when I add lists in to the map (id, subject, sessionTime):

        map.add("11111", "Engineering", "Tue3");
        map.add("11111", "Math", "Wed4");
        map.add("22222", "Engineering", "Wed2");
        map.add("11111", "Engineering", "Thu9");
        map.add("11111", "Physics", "Fri10");
        map.add("22222", "Chemistry", "Wed4");

I want them to be stored in the map like this:

11111 - Engineering - Tue3 & Thu9

11111 - Math - Wed4

11111 - Physics - Fri10

22222 - Engineering - Wed2

22222 - Chemistry - Wed4

or

11111 - Engineering - Tue3 & Thu9 / Math - Wed4 / Physics - Fri10

22222 - Engineering - Wed2 / Chemistry - Wed4

What I have done so far is:

public class test {

    Map<String, arrayList> map;

    public test()
    {
        map = new HashMap<String, arrayList>();
    }

    public Submission add(String staffId, String subject, String sessionTime)
    {

        arrayList t = map.get(staffId);

        if(t == null)
        {
            t = new arrayList();
            map.put(staffId, t);
        }

        return t.Put(subject, new MySubmission(staffId, subject, sessionTime));

    }

    public class arrayList
    {

        List<Submission> list = new ArrayList<Submission>();

        public Submission Put(String subject, MySubmission mySubmission)
        {

            MySubmission submission = new MySubmission();

            submission.setSubject(subject);
            submission.setTime(mySubmission.getTime());

            list.add(submission);

            return submission;
        }
    }
}

I am really getting confused on solving out the challenge... Am I on the right track? Also, how should I write the code to print them all out?

Thanks!

1
  • You should use "11111-Engineering" as the key and the List of <days> as the value in your map. I hope you get the idea Commented Nov 10, 2017 at 9:51

1 Answer 1

2

I think what you need is a Map of Map of List.ie.,

Map< String, Map< String, List< String>>>

Sample code below:

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Test {
public static void main(String[] args) throws IOException {

    Map<String,Map<String,List<String>>> scheduleMap=new HashMap<>();

    //Adding
    addSchedule(scheduleMap, "1111", "Engineering", "Tue3");
    addSchedule(scheduleMap, "1111", "Engineering", "Wed2");
    addSchedule(scheduleMap, "1111", "Math", "Wed4");
    addSchedule(scheduleMap, "1111", "Physics", "Fri10");
    addSchedule(scheduleMap, "2222", "Engineering", "Wed2");
    addSchedule(scheduleMap, "2222", "Chemistry ", "Wed4");

    //Printing
    printSchedule(scheduleMap);

}

private static void printSchedule(Map<String, Map<String, List<String>>> scheduleMap) {
    //Sample printing
    for(Entry<String, Map<String, List<String>>> staff : scheduleMap.entrySet()) {
        for(Entry<String,List<String>> subject:staff.getValue().entrySet()) {
            StringBuilder sb=new StringBuilder();
            sb.append(staff.getKey()).append(" : ").append(subject.getKey()).append(" : ");
            for(String sessionTime: subject.getValue()) {
                sb.append(sessionTime).append(", ");
            }
            sb.setLength(sb.length()-2);
            System.out.println(sb.toString());
        }
    }
}

public static void addSchedule(Map<String,Map<String,List<String>>> scheduleMap, String id,String subject,String sessionTime) {
    if(!scheduleMap.containsKey(id)) {
        scheduleMap.put(id, new HashMap<String,List<String>>());
    }
    if(!scheduleMap.get(id).containsKey(subject)) {
        scheduleMap.get(id).put(subject, new ArrayList<String>());
    }
    scheduleMap.get(id).get(subject).add(sessionTime);
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.