Java 8 - 按 Map 的键和值对列表进行排序
Java 8 - Sort a List by Map's Key and Value
最终目标是针对以下情况对列表进行排序:
我有一个枚举,BLOCKER 是最严重的,MINOR 是最低的。
public enum Severity {
MINOR(0)
MAJOR(1),
BLOCKER(2);
我有一个 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;
}
主要对象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
}
现在,我必须按地图的严重程度对 List<AggregatedData> aggregatedData
进行排序。
aggregatedData.get(0).getSeverityProfile().getSeverities()
有 BLOCKER - 1 和 MAJOR - 2
aggregatedData.get(1).getSeverityProfile().getSeverities()
有 BLOCKER - 3 和 MAJOR - 2 和 MINOR - 4
aggregatedData.get(2).getSeverityProfile().getSeverities()
有 MAJOR - 2 和 MINOR - 8
aggregatedData.get(3).getSeverityProfile().getSeverities()
有 MAJOR - 5 和 MINOR - 10
如果我想要降序值,那么结果应该是:
aggregatedData.get(1)
- BLOCKER - 3 和 MAJOR - 2 和 MINOR - 4
aggregatedData.get(0)
- BLOCKER - 1 和 MAJOR - 2
aggregatedData.get(3)
- 主要 - 5 和次要 - 10
aggregatedData.get(2)
- 主要 - 2 和次要 - 8
(注意 MAJOR 是 BLOCKER 的下一个优先级,在 get(3) 中的计数高于 get(2))
我想做一个Collections.sort(aggregatedData, new Comparator<AggregatedData>() {
问题是,我必须比较 Severity
以及与之关联的值。
我不确定在这种情况下如何构造比较器。
回答我自己的问题:
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;
}
}
我在Collections.sort
中使用了Comparator,并使用了上面的compareTo
方法
最终目标是针对以下情况对列表进行排序:
我有一个枚举,BLOCKER 是最严重的,MINOR 是最低的。
public enum Severity {
MINOR(0)
MAJOR(1),
BLOCKER(2);
我有一个 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;
}
主要对象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
}
现在,我必须按地图的严重程度对 List<AggregatedData> aggregatedData
进行排序。
aggregatedData.get(0).getSeverityProfile().getSeverities()
有 BLOCKER - 1 和 MAJOR - 2
aggregatedData.get(1).getSeverityProfile().getSeverities()
有 BLOCKER - 3 和 MAJOR - 2 和 MINOR - 4
aggregatedData.get(2).getSeverityProfile().getSeverities()
有 MAJOR - 2 和 MINOR - 8
aggregatedData.get(3).getSeverityProfile().getSeverities()
有 MAJOR - 5 和 MINOR - 10
如果我想要降序值,那么结果应该是:
aggregatedData.get(1)
- BLOCKER - 3 和 MAJOR - 2 和 MINOR - 4aggregatedData.get(0)
- BLOCKER - 1 和 MAJOR - 2aggregatedData.get(3)
- 主要 - 5 和次要 - 10aggregatedData.get(2)
- 主要 - 2 和次要 - 8
(注意 MAJOR 是 BLOCKER 的下一个优先级,在 get(3) 中的计数高于 get(2))
我想做一个Collections.sort(aggregatedData, new Comparator<AggregatedData>() {
问题是,我必须比较 Severity
以及与之关联的值。
我不确定在这种情况下如何构造比较器。
回答我自己的问题:
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;
}
}
我在Collections.sort
中使用了Comparator,并使用了上面的compareTo
方法