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)-> 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
No comments:
Add your comment