简体   繁体   中英

how to use hashmap and arraylist in Java

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!

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

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