Sort Array into different arrays based on the number of objects appearing in that array
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
Assume filteredEvents
from below to be a List<Filter>
. Basically a list of objects, each of these holding a list of days in the year, among other not-important-at-the-moment data and a specific colour to draw on the calendar, to represent that there are events for that particular colour distinguishable department.
It's used in the context of filtering a Calendar based on the type of events, "Dept1" is one Filter
, "Dept2" is another Filter
, etc.
Each Filter holds all the dates that should appear in the Calendar for that Department.
The Filter
and Event
classes are at the bottom, for clarification purposes.
Inside the PopulateCalendarWithEvents method I have the following code. What I want to achieve is go through the filterEvents
array of Day
s and sort those based on how many times a given date appears throughout all the Filter
s.
For instance, if Dept2
and Dept5
both have an event for today 16/04/2018, it should put that date into the twoEventDays
only, and put Dept2
and Dept5
's colours correspondingly.
public void PopulateCalendarWithEvents ( List<Filter> filteredEvents)
//Initiate all the arrays to receive the sorted events. 5 as the maximum number of dots per days is set to 5
Collection<CalendarDay> oneEventDays = new ArrayList<>();
int oneColors = new int[1];
Collection<CalendarDay> twoEventDays= new ArrayList<>();
int twoColors= new int[2];
Collection<CalendarDay> threeEventDays= new ArrayList<>();
int threeColors= new int[3];
Collection<CalendarDay> fourEventDays= new ArrayList<>();
int fourColors= new int[4];
Collection<CalendarDay> fiveEventDays= new ArrayList<>();
int fiveColors= new int[5];
//Loop trough all the Filters, and get their Day Arrays
for(int i=1;i<filteredEvents.size();i++)
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
for (int x = 0; x < currentDays.size(); x++)
if (oneEventDays.contains(currentDays.get(x))) //If the currently looped date exists in the first array, then this is it's second or more appearance,
// and should be moved to a higher number array
if (twoEventDays.contains(currentDays.get(x))) //If the currently looped date exists in the 2nd array, it needs to go into the next one
if (threeEventDays.contains((currentDays.get(x))))
if (fourEventDays.contains((currentDays.get(x))))
if (fiveEventDays.contains((currentDays.get(x))))
//As there is room for only so many dots It doesn't matter if I don't add the date and colour here.
// As once you click the date on the Calendar it will show you all events even if they are 100
else
//If the date existed in the 4th List and not the 5th it then goes into the 5th list, including it's colour
fiveEventDays.add(currentDays.get(x));
System.arraycopy(fourColors, 0, fiveColors, 0, fourColors.length);
fiveColors[4] = filteredEvents.get(i).color;
else
//If the date existed in the 3th List and not the 4th it then goes into the 4th list, including it's colour
fourEventDays.add(currentDays.get(x));
System.arraycopy(threeColors, 0, fourColors, 0, threeColors.length);
fourColors[3] = filteredEvents.get(i).color;
else
threeEventDays.add(currentDays.get(x));
System.arraycopy(twoColors, 0, threeColors, 0, twoColors.length);
threeColors[2] = filteredEvents.get(i).color;
else
twoEventDays.add(currentDays.get(x));
System.arraycopy(oneColors, 0, twoColors, 0, oneColors.length);
twoColors[1] = filteredEvents.get(i).color;
else //If the date doesn't exist anywhere, add it into the first List
oneEventDays.add(currentDays.get(x));
oneColors[0] = filteredEvents.get(i).color;
//Remove the higher number of Dates from the lower lists, so that you don't end up with repeating dates for all Lists up to the maximum one, a date should be into
oneEventDays.removeAll(twoEventDays);
twoEventDays.removeAll(threeEventDays);
threeEventDays.removeAll(fourEventDays);
fourEventDays.removeAll(fiveEventDays);
//Send all the Lists and their colors to the Decorator function, that is from the library, loops trough the dates and paints the appropriate number of coloured dots onto each day
calendarView.addDecorator(new EventDecorator(oneEventDays,oneColors));
calendarView.addDecorator(new EventDecorator(twoEventDays,twoColors));
calendarView.addDecorator(new EventDecorator(threeEventDays,threeColors));
calendarView.addDecorator(new EventDecorator(fourEventDays,fourColors));
calendarView.addDecorator(new EventDecorator(fiveEventDays,fiveColors));
This is how the Filter
and Event
classes look, though I'm happy enough with those, I've added them just for clarification.
public class Filter
public int filterIndex;
public String filterName;
public boolean selected;
public ArrayList<Event> eventList;
public int color;
public ArrayList<CalendarDay> calDayArr;
public Filter(int filterIndex, String filterName, boolean selected, ArrayList<Event> eventList, int color)
this.filterIndex = filterIndex;
this.filterName = filterName;
this.selected=selected;
this.eventList=eventList;
this.color = color;
if(eventList!=null)
calDayArr = processCalDays(eventList);
private ArrayList<CalendarDay> processCalDays(ArrayList<Event> eventList)
ArrayList<CalendarDay> calDayArr=new ArrayList<>();
for(int i=0;i<eventList.size();i++)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, eventList.get(i).day);
calendar.set(Calendar.MONTH,eventList.get(i).month);
calendar.set(Calendar.YEAR,eventList.get(i).year);
calDayArr.add(CalendarDay.from(calendar));
return calDayArr;
public class Event
int eventId;
String eventTitle;
Calendar startDate;
Calendar endDate;
int year;
int month;
int day;
public Event(int eventInd, String eventTitle, Calendar startDate, Calendar endDate)
this.eventId=eventInd;
this.eventTitle=eventTitle;
this.startDate=startDate;
this.endDate=endDate;
this.year = startDate.get(Calendar.YEAR);
this.month= startDate.get(Calendar.MONTH);
this.day = startDate.get(Calendar.DAY_OF_MONTH);
Obviously, this is extremely ugly and not at all great way of doing this, but I can't figure out a better way to represent all of this...
This is how the app looks like in action.
- Dept1 has 4 events
- Dept2 has 3 events
- Dept3 has 2 events
- Dept4 has 1 event
Each Dept has it's own colour as can be seen when toggling them.
java android
add a comment |Â
up vote
3
down vote
favorite
Assume filteredEvents
from below to be a List<Filter>
. Basically a list of objects, each of these holding a list of days in the year, among other not-important-at-the-moment data and a specific colour to draw on the calendar, to represent that there are events for that particular colour distinguishable department.
It's used in the context of filtering a Calendar based on the type of events, "Dept1" is one Filter
, "Dept2" is another Filter
, etc.
Each Filter holds all the dates that should appear in the Calendar for that Department.
The Filter
and Event
classes are at the bottom, for clarification purposes.
Inside the PopulateCalendarWithEvents method I have the following code. What I want to achieve is go through the filterEvents
array of Day
s and sort those based on how many times a given date appears throughout all the Filter
s.
For instance, if Dept2
and Dept5
both have an event for today 16/04/2018, it should put that date into the twoEventDays
only, and put Dept2
and Dept5
's colours correspondingly.
public void PopulateCalendarWithEvents ( List<Filter> filteredEvents)
//Initiate all the arrays to receive the sorted events. 5 as the maximum number of dots per days is set to 5
Collection<CalendarDay> oneEventDays = new ArrayList<>();
int oneColors = new int[1];
Collection<CalendarDay> twoEventDays= new ArrayList<>();
int twoColors= new int[2];
Collection<CalendarDay> threeEventDays= new ArrayList<>();
int threeColors= new int[3];
Collection<CalendarDay> fourEventDays= new ArrayList<>();
int fourColors= new int[4];
Collection<CalendarDay> fiveEventDays= new ArrayList<>();
int fiveColors= new int[5];
//Loop trough all the Filters, and get their Day Arrays
for(int i=1;i<filteredEvents.size();i++)
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
for (int x = 0; x < currentDays.size(); x++)
if (oneEventDays.contains(currentDays.get(x))) //If the currently looped date exists in the first array, then this is it's second or more appearance,
// and should be moved to a higher number array
if (twoEventDays.contains(currentDays.get(x))) //If the currently looped date exists in the 2nd array, it needs to go into the next one
if (threeEventDays.contains((currentDays.get(x))))
if (fourEventDays.contains((currentDays.get(x))))
if (fiveEventDays.contains((currentDays.get(x))))
//As there is room for only so many dots It doesn't matter if I don't add the date and colour here.
// As once you click the date on the Calendar it will show you all events even if they are 100
else
//If the date existed in the 4th List and not the 5th it then goes into the 5th list, including it's colour
fiveEventDays.add(currentDays.get(x));
System.arraycopy(fourColors, 0, fiveColors, 0, fourColors.length);
fiveColors[4] = filteredEvents.get(i).color;
else
//If the date existed in the 3th List and not the 4th it then goes into the 4th list, including it's colour
fourEventDays.add(currentDays.get(x));
System.arraycopy(threeColors, 0, fourColors, 0, threeColors.length);
fourColors[3] = filteredEvents.get(i).color;
else
threeEventDays.add(currentDays.get(x));
System.arraycopy(twoColors, 0, threeColors, 0, twoColors.length);
threeColors[2] = filteredEvents.get(i).color;
else
twoEventDays.add(currentDays.get(x));
System.arraycopy(oneColors, 0, twoColors, 0, oneColors.length);
twoColors[1] = filteredEvents.get(i).color;
else //If the date doesn't exist anywhere, add it into the first List
oneEventDays.add(currentDays.get(x));
oneColors[0] = filteredEvents.get(i).color;
//Remove the higher number of Dates from the lower lists, so that you don't end up with repeating dates for all Lists up to the maximum one, a date should be into
oneEventDays.removeAll(twoEventDays);
twoEventDays.removeAll(threeEventDays);
threeEventDays.removeAll(fourEventDays);
fourEventDays.removeAll(fiveEventDays);
//Send all the Lists and their colors to the Decorator function, that is from the library, loops trough the dates and paints the appropriate number of coloured dots onto each day
calendarView.addDecorator(new EventDecorator(oneEventDays,oneColors));
calendarView.addDecorator(new EventDecorator(twoEventDays,twoColors));
calendarView.addDecorator(new EventDecorator(threeEventDays,threeColors));
calendarView.addDecorator(new EventDecorator(fourEventDays,fourColors));
calendarView.addDecorator(new EventDecorator(fiveEventDays,fiveColors));
This is how the Filter
and Event
classes look, though I'm happy enough with those, I've added them just for clarification.
public class Filter
public int filterIndex;
public String filterName;
public boolean selected;
public ArrayList<Event> eventList;
public int color;
public ArrayList<CalendarDay> calDayArr;
public Filter(int filterIndex, String filterName, boolean selected, ArrayList<Event> eventList, int color)
this.filterIndex = filterIndex;
this.filterName = filterName;
this.selected=selected;
this.eventList=eventList;
this.color = color;
if(eventList!=null)
calDayArr = processCalDays(eventList);
private ArrayList<CalendarDay> processCalDays(ArrayList<Event> eventList)
ArrayList<CalendarDay> calDayArr=new ArrayList<>();
for(int i=0;i<eventList.size();i++)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, eventList.get(i).day);
calendar.set(Calendar.MONTH,eventList.get(i).month);
calendar.set(Calendar.YEAR,eventList.get(i).year);
calDayArr.add(CalendarDay.from(calendar));
return calDayArr;
public class Event
int eventId;
String eventTitle;
Calendar startDate;
Calendar endDate;
int year;
int month;
int day;
public Event(int eventInd, String eventTitle, Calendar startDate, Calendar endDate)
this.eventId=eventInd;
this.eventTitle=eventTitle;
this.startDate=startDate;
this.endDate=endDate;
this.year = startDate.get(Calendar.YEAR);
this.month= startDate.get(Calendar.MONTH);
this.day = startDate.get(Calendar.DAY_OF_MONTH);
Obviously, this is extremely ugly and not at all great way of doing this, but I can't figure out a better way to represent all of this...
This is how the app looks like in action.
- Dept1 has 4 events
- Dept2 has 3 events
- Dept3 has 2 events
- Dept4 has 1 event
Each Dept has it's own colour as can be seen when toggling them.
java android
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Assume filteredEvents
from below to be a List<Filter>
. Basically a list of objects, each of these holding a list of days in the year, among other not-important-at-the-moment data and a specific colour to draw on the calendar, to represent that there are events for that particular colour distinguishable department.
It's used in the context of filtering a Calendar based on the type of events, "Dept1" is one Filter
, "Dept2" is another Filter
, etc.
Each Filter holds all the dates that should appear in the Calendar for that Department.
The Filter
and Event
classes are at the bottom, for clarification purposes.
Inside the PopulateCalendarWithEvents method I have the following code. What I want to achieve is go through the filterEvents
array of Day
s and sort those based on how many times a given date appears throughout all the Filter
s.
For instance, if Dept2
and Dept5
both have an event for today 16/04/2018, it should put that date into the twoEventDays
only, and put Dept2
and Dept5
's colours correspondingly.
public void PopulateCalendarWithEvents ( List<Filter> filteredEvents)
//Initiate all the arrays to receive the sorted events. 5 as the maximum number of dots per days is set to 5
Collection<CalendarDay> oneEventDays = new ArrayList<>();
int oneColors = new int[1];
Collection<CalendarDay> twoEventDays= new ArrayList<>();
int twoColors= new int[2];
Collection<CalendarDay> threeEventDays= new ArrayList<>();
int threeColors= new int[3];
Collection<CalendarDay> fourEventDays= new ArrayList<>();
int fourColors= new int[4];
Collection<CalendarDay> fiveEventDays= new ArrayList<>();
int fiveColors= new int[5];
//Loop trough all the Filters, and get their Day Arrays
for(int i=1;i<filteredEvents.size();i++)
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
for (int x = 0; x < currentDays.size(); x++)
if (oneEventDays.contains(currentDays.get(x))) //If the currently looped date exists in the first array, then this is it's second or more appearance,
// and should be moved to a higher number array
if (twoEventDays.contains(currentDays.get(x))) //If the currently looped date exists in the 2nd array, it needs to go into the next one
if (threeEventDays.contains((currentDays.get(x))))
if (fourEventDays.contains((currentDays.get(x))))
if (fiveEventDays.contains((currentDays.get(x))))
//As there is room for only so many dots It doesn't matter if I don't add the date and colour here.
// As once you click the date on the Calendar it will show you all events even if they are 100
else
//If the date existed in the 4th List and not the 5th it then goes into the 5th list, including it's colour
fiveEventDays.add(currentDays.get(x));
System.arraycopy(fourColors, 0, fiveColors, 0, fourColors.length);
fiveColors[4] = filteredEvents.get(i).color;
else
//If the date existed in the 3th List and not the 4th it then goes into the 4th list, including it's colour
fourEventDays.add(currentDays.get(x));
System.arraycopy(threeColors, 0, fourColors, 0, threeColors.length);
fourColors[3] = filteredEvents.get(i).color;
else
threeEventDays.add(currentDays.get(x));
System.arraycopy(twoColors, 0, threeColors, 0, twoColors.length);
threeColors[2] = filteredEvents.get(i).color;
else
twoEventDays.add(currentDays.get(x));
System.arraycopy(oneColors, 0, twoColors, 0, oneColors.length);
twoColors[1] = filteredEvents.get(i).color;
else //If the date doesn't exist anywhere, add it into the first List
oneEventDays.add(currentDays.get(x));
oneColors[0] = filteredEvents.get(i).color;
//Remove the higher number of Dates from the lower lists, so that you don't end up with repeating dates for all Lists up to the maximum one, a date should be into
oneEventDays.removeAll(twoEventDays);
twoEventDays.removeAll(threeEventDays);
threeEventDays.removeAll(fourEventDays);
fourEventDays.removeAll(fiveEventDays);
//Send all the Lists and their colors to the Decorator function, that is from the library, loops trough the dates and paints the appropriate number of coloured dots onto each day
calendarView.addDecorator(new EventDecorator(oneEventDays,oneColors));
calendarView.addDecorator(new EventDecorator(twoEventDays,twoColors));
calendarView.addDecorator(new EventDecorator(threeEventDays,threeColors));
calendarView.addDecorator(new EventDecorator(fourEventDays,fourColors));
calendarView.addDecorator(new EventDecorator(fiveEventDays,fiveColors));
This is how the Filter
and Event
classes look, though I'm happy enough with those, I've added them just for clarification.
public class Filter
public int filterIndex;
public String filterName;
public boolean selected;
public ArrayList<Event> eventList;
public int color;
public ArrayList<CalendarDay> calDayArr;
public Filter(int filterIndex, String filterName, boolean selected, ArrayList<Event> eventList, int color)
this.filterIndex = filterIndex;
this.filterName = filterName;
this.selected=selected;
this.eventList=eventList;
this.color = color;
if(eventList!=null)
calDayArr = processCalDays(eventList);
private ArrayList<CalendarDay> processCalDays(ArrayList<Event> eventList)
ArrayList<CalendarDay> calDayArr=new ArrayList<>();
for(int i=0;i<eventList.size();i++)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, eventList.get(i).day);
calendar.set(Calendar.MONTH,eventList.get(i).month);
calendar.set(Calendar.YEAR,eventList.get(i).year);
calDayArr.add(CalendarDay.from(calendar));
return calDayArr;
public class Event
int eventId;
String eventTitle;
Calendar startDate;
Calendar endDate;
int year;
int month;
int day;
public Event(int eventInd, String eventTitle, Calendar startDate, Calendar endDate)
this.eventId=eventInd;
this.eventTitle=eventTitle;
this.startDate=startDate;
this.endDate=endDate;
this.year = startDate.get(Calendar.YEAR);
this.month= startDate.get(Calendar.MONTH);
this.day = startDate.get(Calendar.DAY_OF_MONTH);
Obviously, this is extremely ugly and not at all great way of doing this, but I can't figure out a better way to represent all of this...
This is how the app looks like in action.
- Dept1 has 4 events
- Dept2 has 3 events
- Dept3 has 2 events
- Dept4 has 1 event
Each Dept has it's own colour as can be seen when toggling them.
java android
Assume filteredEvents
from below to be a List<Filter>
. Basically a list of objects, each of these holding a list of days in the year, among other not-important-at-the-moment data and a specific colour to draw on the calendar, to represent that there are events for that particular colour distinguishable department.
It's used in the context of filtering a Calendar based on the type of events, "Dept1" is one Filter
, "Dept2" is another Filter
, etc.
Each Filter holds all the dates that should appear in the Calendar for that Department.
The Filter
and Event
classes are at the bottom, for clarification purposes.
Inside the PopulateCalendarWithEvents method I have the following code. What I want to achieve is go through the filterEvents
array of Day
s and sort those based on how many times a given date appears throughout all the Filter
s.
For instance, if Dept2
and Dept5
both have an event for today 16/04/2018, it should put that date into the twoEventDays
only, and put Dept2
and Dept5
's colours correspondingly.
public void PopulateCalendarWithEvents ( List<Filter> filteredEvents)
//Initiate all the arrays to receive the sorted events. 5 as the maximum number of dots per days is set to 5
Collection<CalendarDay> oneEventDays = new ArrayList<>();
int oneColors = new int[1];
Collection<CalendarDay> twoEventDays= new ArrayList<>();
int twoColors= new int[2];
Collection<CalendarDay> threeEventDays= new ArrayList<>();
int threeColors= new int[3];
Collection<CalendarDay> fourEventDays= new ArrayList<>();
int fourColors= new int[4];
Collection<CalendarDay> fiveEventDays= new ArrayList<>();
int fiveColors= new int[5];
//Loop trough all the Filters, and get their Day Arrays
for(int i=1;i<filteredEvents.size();i++)
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
for (int x = 0; x < currentDays.size(); x++)
if (oneEventDays.contains(currentDays.get(x))) //If the currently looped date exists in the first array, then this is it's second or more appearance,
// and should be moved to a higher number array
if (twoEventDays.contains(currentDays.get(x))) //If the currently looped date exists in the 2nd array, it needs to go into the next one
if (threeEventDays.contains((currentDays.get(x))))
if (fourEventDays.contains((currentDays.get(x))))
if (fiveEventDays.contains((currentDays.get(x))))
//As there is room for only so many dots It doesn't matter if I don't add the date and colour here.
// As once you click the date on the Calendar it will show you all events even if they are 100
else
//If the date existed in the 4th List and not the 5th it then goes into the 5th list, including it's colour
fiveEventDays.add(currentDays.get(x));
System.arraycopy(fourColors, 0, fiveColors, 0, fourColors.length);
fiveColors[4] = filteredEvents.get(i).color;
else
//If the date existed in the 3th List and not the 4th it then goes into the 4th list, including it's colour
fourEventDays.add(currentDays.get(x));
System.arraycopy(threeColors, 0, fourColors, 0, threeColors.length);
fourColors[3] = filteredEvents.get(i).color;
else
threeEventDays.add(currentDays.get(x));
System.arraycopy(twoColors, 0, threeColors, 0, twoColors.length);
threeColors[2] = filteredEvents.get(i).color;
else
twoEventDays.add(currentDays.get(x));
System.arraycopy(oneColors, 0, twoColors, 0, oneColors.length);
twoColors[1] = filteredEvents.get(i).color;
else //If the date doesn't exist anywhere, add it into the first List
oneEventDays.add(currentDays.get(x));
oneColors[0] = filteredEvents.get(i).color;
//Remove the higher number of Dates from the lower lists, so that you don't end up with repeating dates for all Lists up to the maximum one, a date should be into
oneEventDays.removeAll(twoEventDays);
twoEventDays.removeAll(threeEventDays);
threeEventDays.removeAll(fourEventDays);
fourEventDays.removeAll(fiveEventDays);
//Send all the Lists and their colors to the Decorator function, that is from the library, loops trough the dates and paints the appropriate number of coloured dots onto each day
calendarView.addDecorator(new EventDecorator(oneEventDays,oneColors));
calendarView.addDecorator(new EventDecorator(twoEventDays,twoColors));
calendarView.addDecorator(new EventDecorator(threeEventDays,threeColors));
calendarView.addDecorator(new EventDecorator(fourEventDays,fourColors));
calendarView.addDecorator(new EventDecorator(fiveEventDays,fiveColors));
This is how the Filter
and Event
classes look, though I'm happy enough with those, I've added them just for clarification.
public class Filter
public int filterIndex;
public String filterName;
public boolean selected;
public ArrayList<Event> eventList;
public int color;
public ArrayList<CalendarDay> calDayArr;
public Filter(int filterIndex, String filterName, boolean selected, ArrayList<Event> eventList, int color)
this.filterIndex = filterIndex;
this.filterName = filterName;
this.selected=selected;
this.eventList=eventList;
this.color = color;
if(eventList!=null)
calDayArr = processCalDays(eventList);
private ArrayList<CalendarDay> processCalDays(ArrayList<Event> eventList)
ArrayList<CalendarDay> calDayArr=new ArrayList<>();
for(int i=0;i<eventList.size();i++)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, eventList.get(i).day);
calendar.set(Calendar.MONTH,eventList.get(i).month);
calendar.set(Calendar.YEAR,eventList.get(i).year);
calDayArr.add(CalendarDay.from(calendar));
return calDayArr;
public class Event
int eventId;
String eventTitle;
Calendar startDate;
Calendar endDate;
int year;
int month;
int day;
public Event(int eventInd, String eventTitle, Calendar startDate, Calendar endDate)
this.eventId=eventInd;
this.eventTitle=eventTitle;
this.startDate=startDate;
this.endDate=endDate;
this.year = startDate.get(Calendar.YEAR);
this.month= startDate.get(Calendar.MONTH);
this.day = startDate.get(Calendar.DAY_OF_MONTH);
Obviously, this is extremely ugly and not at all great way of doing this, but I can't figure out a better way to represent all of this...
This is how the app looks like in action.
- Dept1 has 4 events
- Dept2 has 3 events
- Dept3 has 2 events
- Dept4 has 1 event
Each Dept has it's own colour as can be seen when toggling them.
java android
edited Apr 16 at 16:49
Solomon Ucko
822313
822313
asked Apr 16 at 9:46
ÃÂòþ ÃÂõôõò
1285
1285
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
First of all i have a question: How do you know that all the days that have same count also have same colors? For example, day #1 has 3 events from Dept #1, #2, #3, day #2 also has 3 events from Dept #1, #2, #4 so there are 4 colors from two days?
Anyway, looking at the processing that you have, this is how I would do it:
count occurrences for each day: create a
Map
that holdsCalendarDay
as key andList
of colors as value. the size of the list shows how many times the day appeared in the input.after iteration on input is complete we have the required information per day. now we iterate over the map entries and build the collections per count. instead of array of ints I would create a
Set<Integer>
in order to collapse duplicate colors.
Each day would have its own number of colors based on the number of events. Day1 would have colors1,2,3 Day2 will have colors1,2,4 I'll add the app demo in about a minute with a bit more clarification.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:47
oh I see what you mean, good one.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:51
add a comment |Â
up vote
1
down vote
This basically boils down to a grouping problem: you have an input list and want to group it by date, which yields a per-date list of a given length (1 to number of departments). Then, these list's contents get added to a collector list chosen by the list's size, i.e. add all contents of list with size 1 to collector 1, add all contents of lists with size 2 to collector 2, and so on.
Using the stream API, you already have the grouping, the rest is a little footwork:
// example code uses this data class:
private static class EventThingy
public int dept;
public LocalDate date;
public EventThingy(int dept, LocalDate date)
this.dept = dept;
this.date = date;
Collection<EventThingy> in = ...;
// perform grouping:
Map<LocalDate, List<EventThingy>> grouped = in.stream()
.collect(Collectors.groupingBy(evt -> evt.date));
// Prepare target list in map identified by count (5 is departmentCount)
// ... or just put your pre-made lists into such a lookup structure instead
Map<Integer, ArrayList<EventThingy>> countToList = IntStream.rangeClosed(1, 5).boxed()
.collect(Collectors.toMap(Function.identity(), i -> new ArrayList<EventThingy>()));
// Move entries to target lists:
for(List<EventThingy> listAtDate : grouped.values())
ArrayList<EventThingy> targetList = countToList.get(listAtDate.size());
targetList.addAll(listAtDate);
add a comment |Â
up vote
1
down vote
I think @SharonBenAsher's answer is a good starting point for your actual question. I'll instead focus on a few other areas. :)
Declare to the appropriate types, preferring interfaces over implementations
// Not ideal as it's coded against the implementation
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
// Not ideal as Collection is too generic an interface for the usage here
Collection<CalendarDay> oneEventDays = new ArrayList<>();
// Suitable 'middle ground' approach
List<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
List<CalendarDay> oneEventDays = new ArrayList<>();
Don't expose state publicly too easily
CalendarDay.calDayArr
is a public
field, which is not recommended as you are freely allowing callers of a CalendarDay
instance to also freely modify the elements within. Consider a getter getCalendarDays()
that returns an unmodifiable List
of the state, it will be even better if CalendarDay
itself is an immutable class.
public List<CalendarDay> getCalendarDays()
return Collections.unmodifiableList(calendarDays);
for-each
loop
You rely heavily on the regular for (int i = 0; /* ... */)
construct for iteration. You can consider the for-each
style, especially when you don't require knowing the current index.
for(int i=1;i<filteredEvents.size();i++)
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
// ...
// recommended
for (Filter filter : filteredEvents)
List<CalendarDay> currentDays = filter.getCalendarDays();
// ...
Naming and style convention
On a related note, calDayArr
can be better named, and you should standardize where your braces are. In fact, use them even for one-liner if
statements, as the following looked odd at first glance.
if(eventList!=null)
calDayArr = processCalDays(eventList);
// recommended
if (eventList != null)
calendarDays = processCalDays(eventList);
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
First of all i have a question: How do you know that all the days that have same count also have same colors? For example, day #1 has 3 events from Dept #1, #2, #3, day #2 also has 3 events from Dept #1, #2, #4 so there are 4 colors from two days?
Anyway, looking at the processing that you have, this is how I would do it:
count occurrences for each day: create a
Map
that holdsCalendarDay
as key andList
of colors as value. the size of the list shows how many times the day appeared in the input.after iteration on input is complete we have the required information per day. now we iterate over the map entries and build the collections per count. instead of array of ints I would create a
Set<Integer>
in order to collapse duplicate colors.
Each day would have its own number of colors based on the number of events. Day1 would have colors1,2,3 Day2 will have colors1,2,4 I'll add the app demo in about a minute with a bit more clarification.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:47
oh I see what you mean, good one.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:51
add a comment |Â
up vote
3
down vote
First of all i have a question: How do you know that all the days that have same count also have same colors? For example, day #1 has 3 events from Dept #1, #2, #3, day #2 also has 3 events from Dept #1, #2, #4 so there are 4 colors from two days?
Anyway, looking at the processing that you have, this is how I would do it:
count occurrences for each day: create a
Map
that holdsCalendarDay
as key andList
of colors as value. the size of the list shows how many times the day appeared in the input.after iteration on input is complete we have the required information per day. now we iterate over the map entries and build the collections per count. instead of array of ints I would create a
Set<Integer>
in order to collapse duplicate colors.
Each day would have its own number of colors based on the number of events. Day1 would have colors1,2,3 Day2 will have colors1,2,4 I'll add the app demo in about a minute with a bit more clarification.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:47
oh I see what you mean, good one.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:51
add a comment |Â
up vote
3
down vote
up vote
3
down vote
First of all i have a question: How do you know that all the days that have same count also have same colors? For example, day #1 has 3 events from Dept #1, #2, #3, day #2 also has 3 events from Dept #1, #2, #4 so there are 4 colors from two days?
Anyway, looking at the processing that you have, this is how I would do it:
count occurrences for each day: create a
Map
that holdsCalendarDay
as key andList
of colors as value. the size of the list shows how many times the day appeared in the input.after iteration on input is complete we have the required information per day. now we iterate over the map entries and build the collections per count. instead of array of ints I would create a
Set<Integer>
in order to collapse duplicate colors.
First of all i have a question: How do you know that all the days that have same count also have same colors? For example, day #1 has 3 events from Dept #1, #2, #3, day #2 also has 3 events from Dept #1, #2, #4 so there are 4 colors from two days?
Anyway, looking at the processing that you have, this is how I would do it:
count occurrences for each day: create a
Map
that holdsCalendarDay
as key andList
of colors as value. the size of the list shows how many times the day appeared in the input.after iteration on input is complete we have the required information per day. now we iterate over the map entries and build the collections per count. instead of array of ints I would create a
Set<Integer>
in order to collapse duplicate colors.
answered Apr 16 at 10:45
Sharon Ben Asher
2,063512
2,063512
Each day would have its own number of colors based on the number of events. Day1 would have colors1,2,3 Day2 will have colors1,2,4 I'll add the app demo in about a minute with a bit more clarification.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:47
oh I see what you mean, good one.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:51
add a comment |Â
Each day would have its own number of colors based on the number of events. Day1 would have colors1,2,3 Day2 will have colors1,2,4 I'll add the app demo in about a minute with a bit more clarification.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:47
oh I see what you mean, good one.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:51
Each day would have its own number of colors based on the number of events. Day1 would have colors1,2,3 Day2 will have colors1,2,4 I'll add the app demo in about a minute with a bit more clarification.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:47
Each day would have its own number of colors based on the number of events. Day1 would have colors1,2,3 Day2 will have colors1,2,4 I'll add the app demo in about a minute with a bit more clarification.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:47
oh I see what you mean, good one.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:51
oh I see what you mean, good one.
â ÃÂòþ ÃÂõôõò
Apr 16 at 10:51
add a comment |Â
up vote
1
down vote
This basically boils down to a grouping problem: you have an input list and want to group it by date, which yields a per-date list of a given length (1 to number of departments). Then, these list's contents get added to a collector list chosen by the list's size, i.e. add all contents of list with size 1 to collector 1, add all contents of lists with size 2 to collector 2, and so on.
Using the stream API, you already have the grouping, the rest is a little footwork:
// example code uses this data class:
private static class EventThingy
public int dept;
public LocalDate date;
public EventThingy(int dept, LocalDate date)
this.dept = dept;
this.date = date;
Collection<EventThingy> in = ...;
// perform grouping:
Map<LocalDate, List<EventThingy>> grouped = in.stream()
.collect(Collectors.groupingBy(evt -> evt.date));
// Prepare target list in map identified by count (5 is departmentCount)
// ... or just put your pre-made lists into such a lookup structure instead
Map<Integer, ArrayList<EventThingy>> countToList = IntStream.rangeClosed(1, 5).boxed()
.collect(Collectors.toMap(Function.identity(), i -> new ArrayList<EventThingy>()));
// Move entries to target lists:
for(List<EventThingy> listAtDate : grouped.values())
ArrayList<EventThingy> targetList = countToList.get(listAtDate.size());
targetList.addAll(listAtDate);
add a comment |Â
up vote
1
down vote
This basically boils down to a grouping problem: you have an input list and want to group it by date, which yields a per-date list of a given length (1 to number of departments). Then, these list's contents get added to a collector list chosen by the list's size, i.e. add all contents of list with size 1 to collector 1, add all contents of lists with size 2 to collector 2, and so on.
Using the stream API, you already have the grouping, the rest is a little footwork:
// example code uses this data class:
private static class EventThingy
public int dept;
public LocalDate date;
public EventThingy(int dept, LocalDate date)
this.dept = dept;
this.date = date;
Collection<EventThingy> in = ...;
// perform grouping:
Map<LocalDate, List<EventThingy>> grouped = in.stream()
.collect(Collectors.groupingBy(evt -> evt.date));
// Prepare target list in map identified by count (5 is departmentCount)
// ... or just put your pre-made lists into such a lookup structure instead
Map<Integer, ArrayList<EventThingy>> countToList = IntStream.rangeClosed(1, 5).boxed()
.collect(Collectors.toMap(Function.identity(), i -> new ArrayList<EventThingy>()));
// Move entries to target lists:
for(List<EventThingy> listAtDate : grouped.values())
ArrayList<EventThingy> targetList = countToList.get(listAtDate.size());
targetList.addAll(listAtDate);
add a comment |Â
up vote
1
down vote
up vote
1
down vote
This basically boils down to a grouping problem: you have an input list and want to group it by date, which yields a per-date list of a given length (1 to number of departments). Then, these list's contents get added to a collector list chosen by the list's size, i.e. add all contents of list with size 1 to collector 1, add all contents of lists with size 2 to collector 2, and so on.
Using the stream API, you already have the grouping, the rest is a little footwork:
// example code uses this data class:
private static class EventThingy
public int dept;
public LocalDate date;
public EventThingy(int dept, LocalDate date)
this.dept = dept;
this.date = date;
Collection<EventThingy> in = ...;
// perform grouping:
Map<LocalDate, List<EventThingy>> grouped = in.stream()
.collect(Collectors.groupingBy(evt -> evt.date));
// Prepare target list in map identified by count (5 is departmentCount)
// ... or just put your pre-made lists into such a lookup structure instead
Map<Integer, ArrayList<EventThingy>> countToList = IntStream.rangeClosed(1, 5).boxed()
.collect(Collectors.toMap(Function.identity(), i -> new ArrayList<EventThingy>()));
// Move entries to target lists:
for(List<EventThingy> listAtDate : grouped.values())
ArrayList<EventThingy> targetList = countToList.get(listAtDate.size());
targetList.addAll(listAtDate);
This basically boils down to a grouping problem: you have an input list and want to group it by date, which yields a per-date list of a given length (1 to number of departments). Then, these list's contents get added to a collector list chosen by the list's size, i.e. add all contents of list with size 1 to collector 1, add all contents of lists with size 2 to collector 2, and so on.
Using the stream API, you already have the grouping, the rest is a little footwork:
// example code uses this data class:
private static class EventThingy
public int dept;
public LocalDate date;
public EventThingy(int dept, LocalDate date)
this.dept = dept;
this.date = date;
Collection<EventThingy> in = ...;
// perform grouping:
Map<LocalDate, List<EventThingy>> grouped = in.stream()
.collect(Collectors.groupingBy(evt -> evt.date));
// Prepare target list in map identified by count (5 is departmentCount)
// ... or just put your pre-made lists into such a lookup structure instead
Map<Integer, ArrayList<EventThingy>> countToList = IntStream.rangeClosed(1, 5).boxed()
.collect(Collectors.toMap(Function.identity(), i -> new ArrayList<EventThingy>()));
// Move entries to target lists:
for(List<EventThingy> listAtDate : grouped.values())
ArrayList<EventThingy> targetList = countToList.get(listAtDate.size());
targetList.addAll(listAtDate);
answered Apr 16 at 12:29
mtj
2,675212
2,675212
add a comment |Â
add a comment |Â
up vote
1
down vote
I think @SharonBenAsher's answer is a good starting point for your actual question. I'll instead focus on a few other areas. :)
Declare to the appropriate types, preferring interfaces over implementations
// Not ideal as it's coded against the implementation
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
// Not ideal as Collection is too generic an interface for the usage here
Collection<CalendarDay> oneEventDays = new ArrayList<>();
// Suitable 'middle ground' approach
List<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
List<CalendarDay> oneEventDays = new ArrayList<>();
Don't expose state publicly too easily
CalendarDay.calDayArr
is a public
field, which is not recommended as you are freely allowing callers of a CalendarDay
instance to also freely modify the elements within. Consider a getter getCalendarDays()
that returns an unmodifiable List
of the state, it will be even better if CalendarDay
itself is an immutable class.
public List<CalendarDay> getCalendarDays()
return Collections.unmodifiableList(calendarDays);
for-each
loop
You rely heavily on the regular for (int i = 0; /* ... */)
construct for iteration. You can consider the for-each
style, especially when you don't require knowing the current index.
for(int i=1;i<filteredEvents.size();i++)
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
// ...
// recommended
for (Filter filter : filteredEvents)
List<CalendarDay> currentDays = filter.getCalendarDays();
// ...
Naming and style convention
On a related note, calDayArr
can be better named, and you should standardize where your braces are. In fact, use them even for one-liner if
statements, as the following looked odd at first glance.
if(eventList!=null)
calDayArr = processCalDays(eventList);
// recommended
if (eventList != null)
calendarDays = processCalDays(eventList);
add a comment |Â
up vote
1
down vote
I think @SharonBenAsher's answer is a good starting point for your actual question. I'll instead focus on a few other areas. :)
Declare to the appropriate types, preferring interfaces over implementations
// Not ideal as it's coded against the implementation
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
// Not ideal as Collection is too generic an interface for the usage here
Collection<CalendarDay> oneEventDays = new ArrayList<>();
// Suitable 'middle ground' approach
List<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
List<CalendarDay> oneEventDays = new ArrayList<>();
Don't expose state publicly too easily
CalendarDay.calDayArr
is a public
field, which is not recommended as you are freely allowing callers of a CalendarDay
instance to also freely modify the elements within. Consider a getter getCalendarDays()
that returns an unmodifiable List
of the state, it will be even better if CalendarDay
itself is an immutable class.
public List<CalendarDay> getCalendarDays()
return Collections.unmodifiableList(calendarDays);
for-each
loop
You rely heavily on the regular for (int i = 0; /* ... */)
construct for iteration. You can consider the for-each
style, especially when you don't require knowing the current index.
for(int i=1;i<filteredEvents.size();i++)
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
// ...
// recommended
for (Filter filter : filteredEvents)
List<CalendarDay> currentDays = filter.getCalendarDays();
// ...
Naming and style convention
On a related note, calDayArr
can be better named, and you should standardize where your braces are. In fact, use them even for one-liner if
statements, as the following looked odd at first glance.
if(eventList!=null)
calDayArr = processCalDays(eventList);
// recommended
if (eventList != null)
calendarDays = processCalDays(eventList);
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I think @SharonBenAsher's answer is a good starting point for your actual question. I'll instead focus on a few other areas. :)
Declare to the appropriate types, preferring interfaces over implementations
// Not ideal as it's coded against the implementation
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
// Not ideal as Collection is too generic an interface for the usage here
Collection<CalendarDay> oneEventDays = new ArrayList<>();
// Suitable 'middle ground' approach
List<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
List<CalendarDay> oneEventDays = new ArrayList<>();
Don't expose state publicly too easily
CalendarDay.calDayArr
is a public
field, which is not recommended as you are freely allowing callers of a CalendarDay
instance to also freely modify the elements within. Consider a getter getCalendarDays()
that returns an unmodifiable List
of the state, it will be even better if CalendarDay
itself is an immutable class.
public List<CalendarDay> getCalendarDays()
return Collections.unmodifiableList(calendarDays);
for-each
loop
You rely heavily on the regular for (int i = 0; /* ... */)
construct for iteration. You can consider the for-each
style, especially when you don't require knowing the current index.
for(int i=1;i<filteredEvents.size();i++)
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
// ...
// recommended
for (Filter filter : filteredEvents)
List<CalendarDay> currentDays = filter.getCalendarDays();
// ...
Naming and style convention
On a related note, calDayArr
can be better named, and you should standardize where your braces are. In fact, use them even for one-liner if
statements, as the following looked odd at first glance.
if(eventList!=null)
calDayArr = processCalDays(eventList);
// recommended
if (eventList != null)
calendarDays = processCalDays(eventList);
I think @SharonBenAsher's answer is a good starting point for your actual question. I'll instead focus on a few other areas. :)
Declare to the appropriate types, preferring interfaces over implementations
// Not ideal as it's coded against the implementation
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
// Not ideal as Collection is too generic an interface for the usage here
Collection<CalendarDay> oneEventDays = new ArrayList<>();
// Suitable 'middle ground' approach
List<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
List<CalendarDay> oneEventDays = new ArrayList<>();
Don't expose state publicly too easily
CalendarDay.calDayArr
is a public
field, which is not recommended as you are freely allowing callers of a CalendarDay
instance to also freely modify the elements within. Consider a getter getCalendarDays()
that returns an unmodifiable List
of the state, it will be even better if CalendarDay
itself is an immutable class.
public List<CalendarDay> getCalendarDays()
return Collections.unmodifiableList(calendarDays);
for-each
loop
You rely heavily on the regular for (int i = 0; /* ... */)
construct for iteration. You can consider the for-each
style, especially when you don't require knowing the current index.
for(int i=1;i<filteredEvents.size();i++)
ArrayList<CalendarDay> currentDays = filteredEvents.get(i).calDayArr;
// ...
// recommended
for (Filter filter : filteredEvents)
List<CalendarDay> currentDays = filter.getCalendarDays();
// ...
Naming and style convention
On a related note, calDayArr
can be better named, and you should standardize where your braces are. In fact, use them even for one-liner if
statements, as the following looked odd at first glance.
if(eventList!=null)
calDayArr = processCalDays(eventList);
// recommended
if (eventList != null)
calendarDays = processCalDays(eventList);
edited Apr 17 at 2:00
answered Apr 16 at 16:35
h.j.k.
18.1k32490
18.1k32490
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192182%2fsort-array-into-different-arrays-based-on-the-number-of-objects-appearing-in-tha%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password