Sort a List using stream

 Here  in this  exam  simple use  of stream . And another approach to sort . 



import java.util.Arrays;

import java.util.List;

import java.util.stream.Collectors;


public class SortArray {


    public static void main(String[] args) {


   List<string> list = Arrays.asList("9", "A", "YB", "1", "B", "Y", "4", "a", "c" ,"YM");


        /*

List<string> sortedList = list.stream()

.sorted(Comparator.naturalOrder())

.collect(Collectors.toList());


        List<string> sortedList = list.stream()

.sorted((o1,o2)-&gt; o1.compareTo(o2))

.collect(Collectors.toList());

*/


List<string> sortedList = list.stream().sorted().collect(Collectors.toList());


        sortedList.forEach(System.out::println);


    }

}


Program output:--

          

 1

4

9

A

B

Y

YB

YM

a

c

Sort a List using stream Sort  a List  using stream Reviewed by Mukesh Jha on 12:52 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.