0

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 !

1
  • Unclear what you're trying to do. Why do you want to avoid a for each? Commented Sep 5, 2016 at 21:55

3 Answers 3

3

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)));
Sign up to request clarification or add additional context in comments.

3 Comments

I changed my answer to use List#sort instead of Collections.sort for brevity, but it will be the same.
You don’t need a stream().forEach(…) here. You can just call forEach(…) on the List.
@Holger Thanks, I knew there was still something weird with that
0
 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.

1 Comment

Wait, I don't understand. What are you trying to do?
0

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

1 Comment

You should be using Integer.compare. Also, a TreeSet wouldn't allow duplicates and won't be faster than sorting the list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.