deyanmalinov

1. Sort Even Numbers

Jun 3rd, 2019
136
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         String[] line = scan.nextLine().split(", ");
  8.  
  9.         List<Integer> evenNums = Arrays.stream(line)
  10.                 .map(Integer::parseInt)
  11.                 .filter(x -> x%2==0)
  12.                 .collect(Collectors.toList());
  13.         List<String> nums = evenNums.stream()
  14.                 .map(String::valueOf)
  15.                 .collect(Collectors.toList());
  16.  
  17.         String printNum = String.join(", ", nums);
  18.         System.out.println(printNum);
  19.  
  20.         evenNums.sort(Integer::compare);
  21.         List<String> numSorted = evenNums.stream()
  22.                 .map(String::valueOf)
  23.                 .collect(Collectors.toList());
  24.         printNum = String.join(", ", numSorted);
  25.         System.out.println(printNum);
  26. ///////////////////////////////////
  27.         Scanner scan = new Scanner(System.in);
  28.         String[] line= scan.nextLine().split(", ");
  29.         List<Integer> nums = Arrays.stream(line)
  30.                 .map(Integer::parseInt)
  31.                 .collect(Collectors.toList());
  32.  
  33.         nums.removeIf(elem -> elem % 2 != 0);
  34.  
  35.         System.out.println(nums.toString().replaceAll("[\\[\\]]", ""));
  36.  
  37.         nums.sort((a,b) -> a.compareTo(b));
  38.         nums.sort(Integer::compareTo);
  39.  
  40.         System.out.println(nums.toString().replaceAll("[\\[\\]]", ""));
  41. /////////////////////////////////////
  42.     }
  43. }
RAW Paste Data