julien dumortier
julien dumortier

Reputation: 1303

sort list in list with comparator

i would like make a specific sort of my stream with comparator in Java 8.

I have 2 class like this:

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: a1 ; value: 12 ; value: 20 
A: a1 ; value: 2 ; value: 6 ; value: 26 
A: a2 ; value: 3

Thank's !

Upvotes: 0

Views: 128

Answers (3)

castletheperson
castletheperson

Reputation: 33466

If you want a sort that mutates the original List, use List#sort to avoid all the overhead of creating a new list. Afterwards, you can call List#sort on each of the mBObjects.

listOfA.sort(Comparator.comparing(A::getName));
listOfA.forEach(a -> a.getBObjects().sort(Comparator.comparingInt(B::getValue)));

Upvotes: 3

Jude Niroshan
Jude Niroshan

Reputation: 4460

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.

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);
    }
}

Upvotes: 0

Cydhra
Cydhra

Reputation: 581

 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.

Upvotes: 0

Related Questions