Advertisement
another90sm

Untitled

Mar 31st, 2016
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Problem2SortArrayWithStreamAPI {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scann = new Scanner(System.in);
  9.  
  10.         String[] numbers = scann.nextLine().split("\\s+");
  11.         String command = scann.nextLine().toLowerCase();
  12.  
  13.         switch (command) {
  14.             case "ascending":
  15.                 Arrays.stream(numbers)
  16.                         .mapToInt(Integer::parseInt)
  17.                         .boxed()
  18.                         .sorted()
  19.                         .forEach(p -> System.out.print(p + " "));
  20.                 break;
  21.             case "descending":
  22.                 Arrays.stream(numbers)
  23.                         .mapToInt(Integer::parseInt)                //This will also work "p -> Integer.parseInt(p)"
  24.                         .boxed()
  25.                         .sorted((firstNum, secondNum) ->secondNum.compareTo(firstNum))
  26.                         .forEach(p -> System.out.print(p + " "));
  27.                 break;
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement