0

I am trying to group a List of events into dates with a HashMap but i'm having problem achieving it. I want the dates to be the key while the events will be the values. So every events that matches a key will be stored inside a List in the HashMap. So far i have a list of events and a list dates.....my code is show below, thanks in advance.

code

public class TestMap {

public static void main(String[] args){
    TestMap test = new TestMap();

    List<Date> mDays = new ArrayList<Date>();
    SimpleDateFormat mFormat = new SimpleDateFormat("EEEE d MMMM yyyy");
    GregorianCalendar mCalendar = (GregorianCalendar)GregorianCalendar.getInstance();
    mCalendar.setTime(new Date());
     for (int i = 0; i < 7; i++){
            mDays.add(mCalendar.getTime());
            mCalendar.add(Calendar.DAY_OF_WEEK, 1);
           System.out.println("Date: " + mFormat.format(mDays.get(i)));

      }


    List<Events> mEvents = new ArrayList<Events>();
    mEvents.add( new Events("1", "boat party", "Sunday 7 December 2014"));
    mEvents.add( new Events("2", "church meeting", "Monday 8 December 2014"));
    mEvents.add( new Events("3", "shopping", "Saturday 13 December 2014"));
    mEvents.add( new Events("4", "clubbing", "Friday 12 December 2014"));
    mEvents.add( new Events("5", "visiting", "Thursday 11 December 2014"));
    mEvents.add( new Events("6", "tour", "Wednesday 10 December 2014"));
    mEvents.add( new Events("7", "work", "Tuesday 9 December 2014"));
    mEvents.add( new Events("1", "beckie birthday", "Sunday 7 December 2014"));
    mEvents.add( new Events("2", "camping", "Monday 8 December 2014"));
    mEvents.add( new Events("3", "gym", "Saturday 13 December 2014"));
    mEvents.add( new Events("4", "working", "Friday 12 December 2014"));
    mEvents.add( new Events("5", "farming", "Thursday 11 December 2014"));
    mEvents.add( new Events("6", "attend sales", "Wednesday 10 December 2014"));
    mEvents.add( new Events("7", "work", "Tuesday 9 December 2014"));

    for(Events evt: mEvents){

        System.out.println("ID: " +evt.getEventId()+ " Title: "+evt.getEventTitle()+ "   Date:    
  "+evt.getStartDate());
    }
}

}

Events class:

class Events{

private String eventId;
private String eventTitle;
private String startDate;

public Events(String eventId, String eventTitle, String startDate) {
    super();
    this.eventId = eventId;
    this.eventTitle = eventTitle;
    this.startDate = startDate;
}
public String getEventId() {
    return eventId;
}
public void setEventId(String eventId) {
    this.eventId = eventId;
}
public String getEventTitle() {
    return eventTitle;
}
public void setEventTitle(String eventTitle) {
    this.eventTitle = eventTitle;
}
public String getStartDate() {
    return startDate;
}
public void setStartDate(String startDate) {
    this.startDate = startDate;
}

}

2 Answers 2

1

Suppose you are grouping the items by the ID field,

// create a map which relates String-List of Event mapping
HashMap<String, List<Event>> listMap=new HashMap<String,List<Event>>();

// loop through the events
for(Events evt: mEvents){
    if(!listMap.containsKey(evt.getID())){
        listMap.put(evt.getID(), new ArrayList<Event>());
    }
    listMap.get(evt.getID()).add(evt);
}

// if you want to get a list of all events with the same ID,
Collection<Event> eventWithSpecificId=listMap.get("certain ID");
Sign up to request clarification or add additional context in comments.

1 Comment

Please add some explanations.
1

You could use a HashMap<Date,List<Events>> to achieve this.

You get your dates, and for every date object you will get a List<Events> object. After getting this object (or NULL if it does not exist) you will be able to add / remove events and then store them again using put method.

Hope it helps

EDIT

You will have something like this declared in your class:

HashMap<Date,List<Events>> my_events = new HashMap<Date,List<Events>>();

Lets put some examples.

1 - Adding an event to a certain date:

 //suppose that you already have a date in which you want to add the new event

 if(my_events.contains(given_date))
 {
     List<Events> aux = my_events.get(given_date);
     aux.add(new_event);
     my_events.put(given_date,aux);
 }
 else
 {
    List<Events> new_list = new ArrayList<Event>();
    new_list.add(new_event);
    my_events.put(given_date,new_list);

 }

2 - You want to get all the events in a given date:

return my_events.get(given_date); // this will return null if the given date (key) does not exists

2 Comments

thanks zozelfelfo, it helps a bit, but can you pls add a code snippet for more clarification.
Put the answer as correct if this solved your problem, comment anything you need otherwise @mish :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.