0

我有一个代表严重性级别的枚举

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、HIGH、LOW 的顺序?

我能知道我怎么能做到这一点吗?

注意:我正在使用 com.google.common.collect

4

4 回答 4

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
于 2013-07-09T03:42:08.007 回答
1

如果您使用的是 Google Collections,请升级到 Google Guava。使用它的ComparisonChain类。您确定要HIGH, MEDIUM,LOW的顺序吗?反过来更适合 Java 比较。

s如何Person具有严重性级别?也许你的班级应该得到一个更好的名字。

于 2013-07-09T03:19:30.137 回答
1

使用 Comparable 或comparator 然后应用 Collection.sort()

如果使用可比较的接口,则必须实现 compareTo 方法和

Collection.sort(<list>)

如果使用比较器,那么您必须覆盖 compareTo 方法和

Collection.sort(<list>, <comparator>)

以及何时使用 comparatot 或可比较的读取链接:

http://iandjava.blogspot.in/2012/10/comparable-and-comparator.html

于 2013-07-09T03:11:39.133 回答
0

我会做Personimplement 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)]
于 2013-07-09T12:28:00.543 回答