简体   繁体   English

Java如何使用整数集对对象列表进行排序

[英]Java How order a list of objects with sets of integers

I have a list of generic objects with some attributes and one of them it is a Set (treeSet) of Integers. 我有一个带有一些属性的通用对象的列表,其中一个是整数的Set(treeSet)。

class GenericObject{
    Set<Integer> orderNumbers = new TreeSet<>();
    Other attributes...
}

I want to order this list of generic objects comparing the Set... example: 我想订购此通用对象列表,并比较Set ...示例:

My actual List list; 我的实际列表清单;
GenericObject with Set --> 2,3,4 带有Set的GenericObject-> 2,3,4
GenericObject with Set --> 1,2 带有Set的GenericObject-> 1,2
GenericObject with Set --> 4,5 带有Set的GenericObject-> 4,5
GenericObject with Set --> 2,3 带有Set的GenericObject-> 2,3

I want order the list of object like that: 我想要订购这样的对象列表:
1) 1,2 1)1,2
2) 2,3 2)2,3
3) 2,3,4 3)2,3,4
4) 4,5 4)4,5

I am trying to implements the Comparator of GenericObject and Overriding the method compare but I have not got it. 我正在尝试实现GenericObject的比较器,并覆盖比较方法,但是我没有。

Thanks, I'll be waiting for your answers 谢谢,我会等你的答案

Since orderNumbers is a Set the elements will be arranged in their natural order, so you can compare the first elements and size of it. 由于orderNumbers是一个Set因此元素将按其自然顺序排列,因此您可以比较第一个元素及其大小。 Here is the comparator: 这是比较器:

(a, b) -> {

    Integer nexta = a.orderNumbers.iterator().next(),
            nextb = b.orderNumbers.iterator().next();

    if( nexta == nextb ) {
        return a.orderNumbers.size() > b.orderNumbers.size() ? 1 : -1;
    }

    return Integer.compare(nexta, nextb);
}

DEMO DEMO

In the GenericObject Comparator you will have to compare one by one the Integers contained in the Set s. GenericObject Comparator您将必须GenericObject Comparator Set包含的Integer。
You should rely on the Integer comparable method but for the specific case (same elements but one additional), where the set with less elements should be placed before in the order. 您应该依靠Integer可比方法,但对于特定情况(相同元素,但又有一个附加元素),应将具有较少元素的集合放置在顺序的前面。
It should be the most direct efficient approach in terms of time. 就时间而言,它应该是最直接有效的方法。

If performance is not a need (few list and few elements), you could accumulate the Integer s of the List in a String and perform a lexicographical comparison as finally that is what you need. 如果不需要性能(很少的列表和很少的元素),则可以将String的List的Integer累积在String ,并按最终的字典顺序进行比较,这就是您所需要的。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

class GenericObject {

    private Set<Integer> orderNumbers;

    public GenericObject(Set<Integer> treeSet) {
        this.orderNumbers = treeSet;
    }
    // Other attributes...

    public static void main(String[] args) {

        List<GenericObject> list = new ArrayList<>();
        list.add(new GenericObject(new TreeSet<>(Arrays.asList(2, 3, 4))));
        list.add(new GenericObject(new TreeSet<>(Arrays.asList(1, 2))));
        list.add(new GenericObject(new TreeSet<>(Arrays.asList(4, 5))));
        list.add(new GenericObject(new TreeSet<>(Arrays.asList(2, 3))));

        list.sort(new Comparator<GenericObject>() {

            @Override
            public int compare(GenericObject o1, GenericObject o2) {

                String o1String = o1.orderNumbers.stream()
                                                 .map(Object::toString)
                                                 .reduce("", String::concat);

                String o2String = o2.orderNumbers.stream()
                                                 .map(Object::toString)
                                                 .reduce("", String::concat);

                return o1String.compareTo(o2String);
            }
        });

        System.out.println(list);
    }

    @Override
    public String toString() {
        return "GenericObject [orderNumbers=" + orderNumbers + "]";
    }
}

Output : 输出:

[GenericObject [orderNumbers=[1, 2]], GenericObject [orderNumbers=[2, 3]], GenericObject [orderNumbers=[2, 3, 4]], GenericObject [orderNumbers=[4, 5]]] [GenericObject [orderNumbers = [1、2]],GenericObject [orderNumbers = [2、3]],GenericObject [orderNumbers = [2、3、4]],GenericObject [orderNumbers = [4、5]]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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