Java Stream sort operation

Description

Java Stream sort operation


import java.util.ArrayList;
import java.util.stream.Stream;

public class Main {

  public static void main(String[] args) {

    // Create a list of Integer values.
    ArrayList<Integer> myList = new ArrayList<>();
    myList.add(7);/*  www  .j  av a2 s.  c  om*/
    myList.add(8);
    myList.add(1);
    myList.add(2);
    myList.add(7);
    myList.add(5);

    System.out.println("Original list: " + myList);

    Stream<Integer> myStream = myList.stream();

    // Sort the stream by use of sorted().
    Stream<Integer> sortedStream = myStream.sorted();

    // Display the sorted stream by use of forEach().
    System.out.print("Sorted stream: ");
    sortedStream.forEach((n) -> System.out.print(n + " "));
    System.out.println(); 
  }
}
import java.util.Comparator;
import java.util.stream.Stream;

public class Main {

  public static void main(String[] args) {
    Stream<String> s = Stream.of("Groovy","Scala","Ruby","Java","CSS","Kotlin");
    //from   w  w w. ja v  a  2 s.  co m
    Comparator<String> byShares = String.CASE_INSENSITIVE_ORDER;
    s = s.sorted(byShares);
    s.forEach(System.out::println);  
 }
}



PreviousNext

Related