Advertisement
nikeza

8.Custom Comparator

Oct 8th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Collection;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class functional1_Exercises_Custom_Comparator {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         List<Integer> num = Arrays.stream(scanner.nextLine().split("\\s+"))
  12.                 .map(Integer::parseInt)
  13.                 .collect(Collectors.toList());
  14.  
  15.         num.stream().sorted((f, s) -> {
  16.             int result = 0;
  17.             if (f % 2 == 0 && s % 2 != 0) {
  18.                 result = -1;
  19.             } else if (f % 2 != 0 && s % 2 == 0) {
  20.                 result = 1;
  21.             } else {
  22.                 result = f - s;
  23.             }
  24.             return result;
  25.         })
  26.                 .forEach(e -> System.out.print(e + " "));
  27.  
  28.      
  29.     }
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement