input
input

Reputation: 7519

Exclude duplicate values from array

I have an array which returns a set of events for the specific month. Some months can have different events on the same day. How do I detect duplicate dates and show both the events under the same date entries rather than two separate date entries.

Code:

public void setListView(int month, int year, int dv) {

        events = new HolidayEvents();
        _calendar = Calendar.getInstance(Locale.getDefault());
        int totalDays = _calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        for(int i = 1; i <= totalDays; i++){
            if(isHoliday(i, month, year, dv))
            {
                date = i + " " + getMonthForInt(month-1) + " " + year;

                for (Event event : events.eventDetails(this, month, i) 
                {
                       summaries.add(new Summary(date, event.eventdetails));

                } 
            }
        }
}

Output:

11 January has two events. So it displays the result:

11 January 2013 
-Event Name-
11 January 2013
-Event Name-

I would like the output to show like this:

11 January 2013
-Event Name | Event Name-

Upvotes: 0

Views: 128

Answers (2)

MrSmith42
MrSmith42

Reputation: 10151

If you store your Events in in a structure like this:

Map<Date, List<Event>> eventMap= new HashMap<Date,List<Event>>();

you can get all event for a given date d like this:

List<Event> events = eventMap.get(d);

Upvotes: 2

Nicklas Gnejs Eriksson
Nicklas Gnejs Eriksson

Reputation: 3415

ListMultiMap<Date,Event> eventMap = ArrayListMultiMap<Date,Event>.create();

Puts to the multimap will add the event to the list of events for that date. Getting with a date will return a list with all events for the date.

You can also do this manually with a regular map though it is a bit more cumbersome.

http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ArrayListMultimap.html

Upvotes: 0

Related Questions