简体   繁体   English

使用比较器对列表进行排序

[英]sort list in list with comparator

i would like make a specific sort of my stream with comparator in Java 8. 我想用Java 8中的比较器制作一种特定类型的流。

I have 2 class like this: 我有2个这样的课程:

public class A {
    private String mName;
    private List<B> mBObjects;

    public A(String name) {
        mBObjects = new ArrayList<>();
        mName = name;
    }

    public void addValue(B b) {
        mBObjects.add(b);
    }

    public List<B> getBObjects() {
        return mBObjects;
    }

    public String getName() {
        return mName;
    }
}

public class B {
    private int mValue;

    public B(int value) {
        mValue = value;
    }

    public int getValue() {
        return mValue;
    }
}

I initialises theses list with this code: 我用以下代码初始化论文列表:

List<A> listOfA = new ArrayList<>();

//A 1
A a = new A("a1");
a.addValue(new B(20));
a.addValue(new B(12));
listOfA.add(a);

//A 2
a = new A("a2");
a.addValue(new B(3));
listOfA.add(a);


//A 3
a = new A("a1");
a.addValue(new B(2));
a.addValue(new B(26));
a.addValue(new B(6));
listOfA.add(a);

Now i print the lists with this code: 现在,我用以下代码打印列表:

for(A a: listOfA) {
        System.out.print("A: "+a.getName());
        List<B> listOfB = a.getBObjects();
        for(B b:listOfB) {
            System.out.print(" ; notes: "+b.getValue());
        }
        System.out.println(" ");
    }

the result is: 结果是:

A: a1 ; value: 20 ; value: 12 
A: a2 ; value: 3 
A: a1 ; value: 2 ; value: 26 ; value: 6

I add this code after: 我在以下位置添加此代码:

listOfA = listOfA.stream().sorted(Comparator.comparing(A::getName)).collect(Collectors.toList());

The new result of this lists is: 此列表的新结果是:

A: a1 ; value: 20 ; value: 12 
A: a1 ; value: 2 ; value: 26 ; value: 6 
A: a2 ; value: 3

For each A object, i would like sort the list mBObjects without make a foreach to obtain this result: 对于每个A对象,我想对列表mBObjects进行排序,而无需进行foreach获取此结果:

A: a1 ; value: 12 ; value: 20 
A: a1 ; value: 2 ; value: 6 ; value: 26 
A: a2 ; value: 3

Thank's ! 谢谢 !

If you want a sort that mutates the original List , use List#sort to avoid all the overhead of creating a new list. 如果您想要使原始List发生变异的排序,请使用List#sort避免创建新列表的所有开销。 Afterwards, you can call List#sort on each of the mBObjects . 之后,您可以在每个mBObjects上调用List#sort

listOfA.sort(Comparator.comparing(A::getName));
listOfA.forEach(a -> a.getBObjects().sort(Comparator.comparingInt(B::getValue)));
 for(B b:listOfB) {
     System.out.print(" ; notes: "+b.getValue());
 }

Could be changed into 可以改为

listOfB.stream().sorted(Comperator.comparing(B::getValue)).foreach(note -> System.out.print(" ; notes: " + note.getValue());

I think this should fit your question. 我认为这应该适合您的问题。

As you are just looking to sort the B objects in that List<B> mBObjects , it would be nice if you just make your B s comparable and add them to a TreeSet instead of an ArrayList . 当您只想对List<B> mBObjectsB对象进行排序时,如果仅使B 您的B 相当,然后将它们添加到TreeSet而不是ArrayList ,那就太好了。

mBObjects = new TreeSet<B>();

public class B implements Comparable<B>{
    private int mValue;

    //...
    public int compareTo(B b1){
       return this.mValue < b1.mValue? 1 : (this.mValue > b1.mValue? -1:0);
    }
}

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

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