-1

I want to order information about events I got as HTML from a website (Category, multiple events in that category, information about one specific event) in a big HashMap and what I tried looks like this:

HashMap categoryMap = new HashMap();
HashMap eventMap = new HashMap();
HashMap singleEventMap = new HashMap();
categoryMap.put(eventCategory, eventMap);
eventMap.put(eventTitle, singleEventMap);
singleEventMap.put("starttime", eventTime);
singleEventMap.put("location", eventLocation);
singleEventMap.put("description", eventDescription);

I`m used to python dictonaries and can't find, how I can add another event to the category or how I can access the stored information in Java. I would be glad if anyone could give me a code example or a link with a similar problem or a good explanation.

Laura
  • 11
  • Check out [stack doc](http://stackoverflow.com/documentation/java/90/collections/12413/usage-of-hashmap) – Jorn Vernee Jan 04 '17 at 15:35
  • Thank You, I found that one, too. How do I add another event to the _eventMap_, which is the value of the _eventCategory_? **categoryMap(eventCategory, eventMap.put(eventTitle,singleEventMap))** ? Because this doesn't work. Sorry, if my question is stupid. I seem to be unable to see the easy solution. – Laura Jan 04 '17 at 15:45

1 Answers1

1

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));
Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 1
    You were 1 second earlier than me :) I would also suggest encapsulating the category map in it's own class (`Catalog`) with an `addEvent` and `getEvent` method. It also seems to make sense for `category` and `title` to be fields of `Event`. – Jorn Vernee Jan 04 '17 at 16:04
  • Thank you! I totally forgot that I already made an events class! As you already guessed, I am not a Java expert and I really appreciate your detailed explanation. – Laura Jan 04 '17 at 16:09