How to print sorted stream in descending order with Java

1 Answer

0 votes
import java.util.stream.Stream;

public class MyClass {
    public static void main(String args[]) {
        Stream<Integer> stream = Stream.of(4, 6, 0, 7, 3, 1); 

        stream.sorted((a, b)->b - a).forEach(System.out::println);
    }
}




/*
run:

7
6
4
3
1
0

*/

 



answered Jun 6, 2023 by avibootz

Related questions

1 answer 173 views
1 answer 236 views
1 answer 284 views
1 answer 234 views
1 answer 228 views
1 answer 240 views
...