I have an ArrayList (Result) where the Result objects all contain a reference to the Event object. An ArrayList can have 50 Result objects, but only 5 different Event objects. Is there a way to iterate over an ArrayList and group together Results with identical Event.getName () links?
I want to separately run the method according to the results in the high jump event, and then only according to the results in the long jump event, etc. I will not know in advance which events my list of results has, since they are created from user input.
I know how to sort an ArrayList by event name, but I want to rather split the list by event and save them in other temporary lists (?)
from the result class:
public Result(double result, int attemptNumber, Participant participant, Event event) {
this.result = result;
this.attemptNumber = attemptNumber;
this.participant = participant;
this.event = event;
}
from the Event class:
public Event (String eventName, int attemptsAllowed) {
this.eventName = eventName;
this.attemptsAllowed = attemptsAllowed;
}
public String getEventName() {
return eventName;
}
ArrayList allResults, , allResults ( resultByEvent) :
public ArrayList<Result> resultsByEvent(String eventName) {
resultsByEvent.addAll(allResults);
for(int i = 0; i < resultsByEvent.size(); i++) {
Event event = resultsByEvent.get(i).getEvent();
if(!event.getEventName().equals(eventName)) {
resultsByEvent.remove(i);
}
}
Collections.sort(resultsByEvent, new EventLeaderboard());
for(int n = 0; n < resultsByEvent.size(); n++) {
for(int j = n + 1; j < resultsByEvent.size(); j++) {
Participant participant1 = resultsByEvent.get(n).getParticipant();
Participant participant2 = resultsByEvent.get(j).getParticipant();
if(participant1.getParticipantId() == participant2.getParticipantId()) {
resultsByEvent.remove(j);
j = j - 1;
}
}
}
return resultsByEvent;
}
- , .