veronikaaa86

Change List

Feb 11th, 2018
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. package Lists;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. import java.util.stream.Collectors;
  9.  
  10. public class P02_ChangeList {
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  13.  
  14.         int[] numbersArr = Arrays.stream(reader.readLine()
  15.                 .split("\\s+"))
  16.                 .mapToInt(Integer::parseInt)
  17.                 .toArray();
  18.  
  19.         List<Integer> numsList = Arrays.stream(numbersArr).boxed().collect(Collectors.toList());
  20.  
  21.         String command = "";
  22.         String[] sequence;
  23.         while (!"Even".equals(command) && !"Odd".equals(command)) {
  24.             sequence = reader.readLine().split("\\s+");
  25.             command = sequence[0];
  26.             int value = 0;
  27.             int index = 0;
  28.  
  29.             if ("Delete".equals(command)) {
  30.                 value = Integer.parseInt(sequence[1]);
  31.                 numsList.removeAll(Arrays.asList(value));
  32.             } else if ("Insert".equals(command)) {
  33.                 value = Integer.parseInt(sequence[1]);
  34.                 index = Integer.parseInt(sequence[2]);
  35.                 numsList.add(index, value);
  36.             }
  37.         }
  38.         if ("Odd".equals(command)) {
  39.             numsList
  40.                     .stream()
  41.                     .filter(n -> n % 2 != 0)
  42.                     .map(n -> n.toString())
  43.                     .forEach(n -> System.out.printf("%s ", n));
  44.         } else if ("Even".equals(command)) {
  45.             numsList
  46.                     .stream()
  47.                     .filter(n -> n % 2 == 0)
  48.                     .map(n -> n.toString())
  49.                     .forEach(n -> System.out.printf("%s ", n));
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment