简体   繁体   中英

Java 8 - Sort a List by Map's Key and Value

End goal is to sort the list for the following scenario:

I have an enum with the BLOCKER being the highest severity an MINOR being the lowest.

public enum Severity {
    MINOR(0)
    MAJOR(1),
    BLOCKER(2);

I have a class SeverityProfile

public class SeverityProfile {

    private final Map<Severity, Integer> severities;

    public SeverityProfile(Map<Severity, Integer> severities) {
        this.severities = Collections.unmodifiableMap(severities);
    }

    public Map<Severity, Integer> getSeverities() {
        return severities;
    }

The main object AggregatedData


public class AggregatedData {

 
    private final SeverityProfile severityProfile;
    ... other private variables

    public AggregatedData(SeverityProfile severityProfile) {

        this.severityProfile = severityProfile;
  
    }


    public SeverityProfile getSeverityProfile() {
        return severityProfile;
    }
    ... other getters
}

Now, I have to sort the List<AggregatedData> aggregatedData by the Severity of the map. aggregatedData.get(0).getSeverityProfile().getSeverities() has BLOCKER - 1 and MAJOR - 2 aggregatedData.get(1).getSeverityProfile().getSeverities() has BLOCKER - 3 and MAJOR - 2 and MINOR - 4 aggregatedData.get(2).getSeverityProfile().getSeverities() has MAJOR - 2 and MINOR - 8 aggregatedData.get(3).getSeverityProfile().getSeverities() has MAJOR - 5 and MINOR - 10

If I want the descending values, then the result should be:

  • aggregatedData.get(1) - BLOCKER - 3 and MAJOR - 2 and MINOR - 4
  • aggregatedData.get(0) - BLOCKER - 1 and MAJOR - 2
  • aggregatedData.get(3) - MAJOR - 5 and MINOR - 10
  • aggregatedData.get(2) - MAJOR - 2 and MINOR - 8

(Notice MAJOR which is next priority to BLOCKER has higher count in get(3) than get(2))

I'd like to do a Collections.sort(aggregatedData, new Comparator<AggregatedData>() {

Problem is, I have to compare the Severity and also the values associated with it. I'm not sure how to construct the Comparator in this case.

Answering my own question:

public class SeverityProfile implements Comparable<SeverityProfile> {

    private final Map<Severity, Integer> severities;

    private final int[] counts;

    public SeverityProfile(Map<Severity, Integer> severities) {
        counts = new int[Severity.values().length];

        this.severities = Collections.unmodifiableMap(severities);

        counts[Severity.BLOCKER.ordinal()] = severities.getOrDefault(Severity.BLOCKER, 0);
        counts[Severity.MAJOR.ordinal()] = severities.getOrDefault(Severity.MAJOR, 0);
        counts[Severity.MINOR.ordinal()] = severities.getOrDefault(Severity.MINOR, 0);
    }


    public Map<Severity, Integer> getSeverities() {
        return severities;
    }

    public int[] getCounts() {
        return counts;
    }

    @Override
    public int compareTo(SeverityProfile other) {
        if (other == this) {
            return 0;
        }

        for (Severity severity : Severity.getSeveritiesByDescendingRank()) {
            if (getCounts()[severity.ordinal()] != other.getCounts()[severity.ordinal()]) {
                return getCounts()[severity.ordinal()] - other.getCounts()[severity.ordinal()];

            }
        }
        return 0;
    }
}

I used Comparator in the Collections.sort and used the above compareTo method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM