fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.stream.*;
  7. import java.util.function.*;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. private static List<String> skipElements1(List<String> elements, Set<String> elementsToDelete) {
  13. ListIterator<String> iterator = elements.listIterator();
  14. String element;
  15. while (iterator.hasNext()) {
  16. element = iterator.next();
  17. if (element.equals("D")) {
  18. iterator.previous();
  19. iterator.add("G");
  20. iterator.next();
  21. }
  22.  
  23. if (elementsToDelete.contains(element)) {
  24. iterator.remove();
  25. }
  26. }
  27. return elements;
  28. }
  29.  
  30. private static List<String> skipElements2(List<String> inputElements, Set<String> elementsToDelete) {
  31. return inputElements.stream()
  32. .flatMap(entry -> "D".equals(entry) ? Stream.of("G", entry) : Stream.of(entry))
  33. .filter(Predicate.not(elementsToDelete::contains)).collect(Collectors.toList());
  34. }
  35.  
  36. static void test(int testId, List<String> inputElements, Set<String> skipElements) {
  37. List<String> result1;
  38. List<String> result2;
  39. try {
  40. List<String> workCopy = new ArrayList(inputElements);
  41. result1 = skipElements1(workCopy, skipElements);
  42. } catch(Exception e) {
  43. System.err.println(String.format("Test %d failed in ver1 - %s", testId, e));
  44. return;
  45. }
  46.  
  47. try {
  48. List<String> workCopy = new ArrayList(inputElements);
  49. result2 = skipElements2(workCopy, skipElements);
  50. } catch(Exception e) {
  51. System.err.printf("Test %d failed in ver2 - %s\n", testId, e);
  52. return;
  53. }
  54.  
  55. boolean equal = result1.toString().equals(result2.toString());
  56. if (equal) {
  57. System.out.printf("Test %d: OK\n", testId);
  58. } else {
  59. System.out.printf("Test %d: failed (%s <> %s)\n", testId, result1, result2);
  60. }
  61. }
  62.  
  63. public static void main (String[] args) throws java.lang.Exception
  64. {
  65. Set<String> elementsToDelete = Set.of("A", "C", "E");
  66.  
  67. List<String> elements = Stream.of("A", "B", "C", "D", "E")
  68. .collect(Collectors.toCollection(LinkedList::new));
  69.  
  70. test(1, List.of("A", "B", "C", "D", "E"), Set.of("A", "C", "E"));
  71. test(2, List.of("D", "A", "B", "C", "D", "E"), Set.of("A", "C", "E"));
  72. test(3, List.of("A", "B", "C", "D", "E"), Set.of("A", "C", "E", "D"));
  73. test(4, List.of("D", "E"), Set.of("A", "C", "E"));
  74. test(5, List.of("E", "D"), Set.of("A", "C", "E"));
  75. test(6, Collections.emptyList(), Set.of("A", "C", "E"));
  76. test(7, List.of("D"), Set.of("A", "C", "E"));
  77. test(8, List.of("D", "D"), Set.of("A", "C", "E"));
  78. test(9, List.of("D", "D", "D"), Set.of("A", "C", "E"));
  79. }
  80. }
Success #stdin #stdout 0.09s 49104KB
stdin
Standard input is empty
stdout
Test 1: OK
Test 2: OK
Test 3: OK
Test 4: OK
Test 5: OK
Test 6: OK
Test 7: OK
Test 8: OK
Test 9: OK