如何通过键列表对Hashmap中的List元素进行分组

时间:2014-12-07 11:24:54

标签: java android data-structures arraylist hashmap

我正在尝试使用HashMap将事件列表分组到日期中,但我在实现它时遇到了问题。我希望日期是关键,而事件将是价值观。因此,与键匹配的每个事件都将存储在HashMap中的List中。到目前为止,我有一个事件列表和一个列表日期.....我的代码如下所示,提前感谢。

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

}

活动类:

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 个答案:

答案 0 :(得分:1)

您可以使用HashMap<Date,List<Events>>来实现此目标。

您可以获得日期,并且对于每个日期对象,您将获得List<Events>个对象。获取此对象后(如果不存在则为NULL),您将能够添加/删除事件,然后使用put方法再次存储它们。

希望有所帮助

修改

你的班级会宣布这样的事情:

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

让我们举一些例子。

1 - 将事件添加到特定日期:

 //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 - 你想获得给定日期的所有事件:

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

答案 1 :(得分:0)

假设您按ID字段对项目进行分组,

// 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");