我有一个表示严重性级别的枚举
public enum Severity {
HIGH("H"), MEDIUM("M"), LOW("L");
}
Person one = new Person();
one.setSeverity(Severity.HIGH);
其他领域......
Person two = new Person();
two.setSeverity(Severity.LOW);
.....
Person three = new Person();
three.setSeverity(Severity.HIGH);
List<Person> persons = Lists.newArrayList();
persons.add(one);
persons.add(two);
persons.add(three);
我想根据严重性字段对人员列表进行排序(即HIGH,MEDIUM然后LOW)。
排序人员名单后的预期结果应该是HIGH,HIGH,LOW?
我可以知道如何实现这一目标吗?
注意:我正在使用com.google.common.collect
答案 0 :(得分:2)
尝试以下代码
创建ENUM
package com.rais;
public enum Severity {
HIGH("H"), MEDIUM("M"), LOW("L");
private final String value;
private Severity(String value) {
this.value = value;
}
}
现在根据您的要求创建Person
课程,例如
package com.rais;
public class Person {
private Severity severity;
private String name;
public Person(Severity severity, String name) {
super();
this.severity = severity;
this.name = name;
}
public Severity getSeverity() {
return severity;
}
public void setSeverity(Severity severity) {
this.severity = severity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
最后创建一个测试客户端并应用以下逻辑。
package com.rais;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestClient {
public static void main(String[] args) {
Person one = new Person(Severity.HIGH, "shayam");
Person two = new Person(Severity.MEDIUM, "mohan");
Person three = new Person(Severity.LOW, "radha");
Person four = new Person(Severity.HIGH, "rakesh");
Person five = new Person(Severity.MEDIUM, "kailash");
Person six = new Person(Severity.LOW, "rais");
Person seven = new Person(Severity.LOW, "abhishek");
List<Person> persons = new ArrayList<Person>();
persons.add(one);
persons.add(two);
persons.add(three);
persons.add(four);
persons.add(five);
persons.add(six);
persons.add(seven);
Collections.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person person1, Person person2) {
if(person1.getSeverity()==person2.getSeverity())
{
return person1.getName().compareTo(person2.getName());
}
else{
return person1.getSeverity().compareTo(person2.getSeverity());
}
}
});
for (Person person : persons) {
System.out.println(person.getName()+" "+ person.getSeverity());
}
}
}
我相信你会得到低于输出。
rakesh HIGH
shayam HIGH
kailash MEDIUM
mohan MEDIUM
abhishek LOW
radha LOW
rais LOW
答案 1 :(得分:1)
使用Comparable或者比较器然后申请
Collection.sort()
。
如果使用类似的接口,则必须实现compareTo方法和
Collection.sort(<list>)
如果使用比较器,则必须覆盖compareTo方法和
Collection.sort(<list>, <comparator>)
何时使用comparatot或类似的读取链接:
http://iandjava.blogspot.in/2012/10/comparable-and-comparator.html
答案 2 :(得分:1)
如果您使用的是Google Collections,请升级到Google Guava。使用其ComparisonChain课程。您确定要按此顺序HIGH
,MEDIUM
,LOW
吗?反过来更适合Java比较。
Person
的严重程度如何?也许你的班级值得一个更好的名字。
答案 3 :(得分:0)
我会Person
实现Comparable
,这使得排序代码非常简单和简洁。
请注意,枚举是隐式可比的:
public enum Severity {
HIGH("H"), MEDIUM("M"), LOW("L");
private final String code;
private Severity(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
public class Person implements Comparable<Person> {
private Severity severity;
private final String name;
public Person(Severity severity, String name) {
this.severity = severity;
this.name = name;
}
public Severity getSeverity() {
return severity;
}
public void setSeverity(Severity severity) {
this.severity = severity;
}
public String getName() {
return name;
}
@Override
public int compareTo(Person person) {
return severity == person.severity ? name.compareTo(person.name)
: severity.compareTo(person.severity);
}
@Override
public String toString() {
return name + "(" + severity +")";
}
}
现在有一些测试代码:
Person one = new Person(Severity.HIGH, "one");
Person two = new Person(Severity.LOW, "two");
Person three = new Person(Severity.HIGH, "three");
List<Person> persons = new ArrayList<Person>();
persons.add(one);
persons.add(two);
persons.add(three);
Collections.sort(persons);
System.out.println(persons);
输出:
[one(HIGH), three(HIGH), two(LOW)]