Group list of compound objects

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

- , .

+4
3

, API Stream:

Map<String, List<Event>> eventsGroupedByName = allResults.stream().collect(
                Collectors.groupingBy(e -> e.getEvent().getName()));

Map<String, List<Result>> resultsGroupedByEventName = allResults.stream().collect(
                Collectors.groupingBy(r -> r.getEvent().getName()));
+4

eventResults Map " " / " ", , , , List<Result> matchingResults = eventsResults.get(<some event name>).

results List<Result>.

        Map<String,List<Result>> eventsResults = new HashMap<String,List<Result>>();

        for(Result result : results){

            String eventName = result.getEvent().getEventName();
            // search the map for this event name
            List<Result> storedResultsList = eventsResults.get(eventName);

            if(storedResultsList == null){
                // no entry for this name yet, let create one
                List<Result> newEventResultsList = new ArrayList<Result>();
                newEventResultsList.add(result);

                eventsResults.put(eventName, newEventResultsList);


            }
            else{
                // entry found, let add this result to the list
                storedResultsList.add(result);
            }
        }
+3

You can make map listings. Key = reference, data = lists of this type of links.

0
source

Source: https://habr.com/ru/post/1667993/


All Articles