java过滤器和拆分ArrayList
在我的程序中,我有一个ArrayList of Events
。以下是我的代码节选:
ArrayList<Event> events;
public class Event
{
// with getter and setter - don't write it down to keeps this short
protected int day, month, year;
}
public static void main(String args[])
{
events = new ArrayList<Event>();
// fill list with items
HashMap<String, ArrayList<Event>> map = orderEvents(3, 2014);
// some more code
}
private HashMap<String, ArrayList<Event>> orderEvents(int month, int year)
{
// should return all events from the given month and year in a HashMap
// HashMap has this format: key = day | value = events of this day
}
当然,现在我可以循环浏览整个列表,处理很多if conditions
,但我认为我没有最有效的解决方案,对其他人来说,这将很难阅读。有没有一些方法可以用来过滤ArrayList,或者根据ArrayList中对象的值将其拆分
# 1 楼答案
这是我的解决方案
下面是一个使用
MyEvent
类的示例这是输出