1) Do not use raw generic types.
Always specify the type arguments. You should also program to the interface. E.g.
Map<String, Map<String, Map<String, Object>>> categoryMap = new HashMap<>();
Map<String, Map<String, Object>> eventMap = new HashMap<>();
Map<String, Object> singleEventMap = new HashMap<>();
2) Java is an Object-Oriented language, use it.
E.g. create an Event
class with fields starttime
, location
, and description
.
public class Event {
private final LocalDateTime starttime;
private final String location;
private final String description;
public Event(LocalDateTime starttime, String location, String description) {
this.starttime = starttime;
this.location = location;
this.description = description;
}
public LocalDateTime getStarttime() {
return this.starttime;
}
public String getLocation() {
return this.location;
}
public String getDescription() {
return this.description;
}
}
Then use:
Map<String, Map<String, Event>> categoryMap = new HashMap<>();
Map<String, Event> eventMap = new HashMap<>();
3) To add another event:
Create another instance of singleEventMap
, add the properties and add it to the eventMap
.
Your way:
HashMap categoryMap = new HashMap();
HashMap eventMap = new HashMap();
categoryMap.put(eventCategory, eventMap);
Map singleEventMap = new HashMap();
eventMap.put(eventTitle1, singleEventMap);
singleEventMap.put("starttime", starttime1);
singleEventMap.put("location", location1);
singleEventMap.put("description", description1);
singleEventMap = new HashMap();
eventMap.put(eventTitle2, singleEventMap);
singleEventMap.put("starttime", starttime2);
singleEventMap.put("location", location2);
singleEventMap.put("description", description2);
The Java way:
Map<String, Map<String, Event>> categoryMap = new HashMap<>();
Map<String, Event> eventMap = new HashMap<>();
categoryMap.put(eventCategory, eventMap);
eventMap.put(eventTitle1, new Event(starttime1, location1, description1));
eventMap.put(eventTitle2, new Event(starttime2, location2, description2));
Or if they have different categories:
Map<String, Map<String, Event>> categoryMap = new HashMap<>();
Map<String, Event> eventMap1 = new HashMap<>();
categoryMap.put(eventCategory1, eventMap1);
eventMap1.put(eventTitle1, new Event(starttime1, location1, description1));
Map<String, Event> eventMap2 = new HashMap<>();
categoryMap.put(eventCategory2, eventMap2);
eventMap2.put(eventTitle2, new Event(starttime2, location2, description2));