根据每个成员的字段对列表进行排序

时间:2020-12-29 18:05:19

Suppose I have the following List, which contains items of type ENTITY. ENTITY has an integer field which determines its natural ordering. I want to get the ENTITY which is the maximum or minimum based on that field's value. How can I implement this in Java?

假设我有以下列表,其中包含ENTITY类型的项目。 ENTITY有一个整数字段,用于确定其自然顺序。我想获得ENTITY,它是基于该字段值的最大值或最小值。我怎样才能在Java中实现它?

List<ENTITY> lt = new ArrayList<ENTITY>();

class ENTITY
{
    int field;
    /* Constructor, getters, setters... */
}

2 个解决方案

#1


1  

//stream the elements, map to their fields, and get the max
return lt.stream().max((e1, e2) -> Integer.compare(e1.filed, e2.filed)).orElse(/* default */);

Just one of the many applications of Java 8's stream api.

只是Java 8的流API的众多应用程序之一。

Though I would suggest working on some coding conventions first.

虽然我建议先编写一些编码约定。

#2


2  

Use Collections.sort with a Comparator to sort your list. Depending on whether you sort ascending or descending, the positions of the max and min elements will differ. In either case, they will be at opposite ends, one at the top of the list and one at the bottom. To sort in ascending order (smallest element at the beginning of the list, largest at the end) you can use something like this:

将Collections.sort与Comparator一起使用以对列表进行排序。根据您是按升序还是按升序排序,max和min元素的位置会有所不同。在任何一种情况下,它们都在相反的两端,一个在列表的顶部,一个在底部。要按升序排序(列表开头的最小元素,最后的最大元素),您可以使用以下内容:

Collections.sort(lt, new Comparator<ENTITY> {
    public int compare(ENTITY o1, ENTITY o2) {
        if (o1 == null) {
            if (o2 == null) {
                return 0;
            }
            return -1;
        }
        else if (o2 == null) {
            return 1;
        }

        // If field is Comparable:
        return o1.getField().compareTo(o2.getField());

        // OR - If field is an int
        return o1.getField() < o2.getField() ? -1 : (o1.getField() > o2.getField() ? 1 : 0);
    }
});

#1


1  

//stream the elements, map to their fields, and get the max
return lt.stream().max((e1, e2) -> Integer.compare(e1.filed, e2.filed)).orElse(/* default */);

Just one of the many applications of Java 8's stream api.

只是Java 8的流API的众多应用程序之一。

Though I would suggest working on some coding conventions first.

虽然我建议先编写一些编码约定。

#2


2  

Use Collections.sort with a Comparator to sort your list. Depending on whether you sort ascending or descending, the positions of the max and min elements will differ. In either case, they will be at opposite ends, one at the top of the list and one at the bottom. To sort in ascending order (smallest element at the beginning of the list, largest at the end) you can use something like this:

将Collections.sort与Comparator一起使用以对列表进行排序。根据您是按升序还是按升序排序,max和min元素的位置会有所不同。在任何一种情况下,它们都在相反的两端,一个在列表的顶部,一个在底部。要按升序排序(列表开头的最小元素,最后的最大元素),您可以使用以下内容:

Collections.sort(lt, new Comparator<ENTITY> {
    public int compare(ENTITY o1, ENTITY o2) {
        if (o1 == null) {
            if (o2 == null) {
                return 0;
            }
            return -1;
        }
        else if (o2 == null) {
            return 1;
        }

        // If field is Comparable:
        return o1.getField().compareTo(o2.getField());

        // OR - If field is an int
        return o1.getField() < o2.getField() ? -1 : (o1.getField() > o2.getField() ? 1 : 0);
    }
});