P08 { public static <T> List<T> compress(List<T> list) { Objects.requireNonNull(list, "Please make sure list is not null"); List<T> result = new ArrayList<>(); T lastElement = null; for (T e : list) { if (!Objects.equals(lastElement, e)) { result.add(e); } lastElement = e; } return result; }  static List<T> compress(List<T> list);  }
@Test public void shouldRemoveConsecutiveDuplicatesInAList() throws Exception { List<String> compressedList = P08.compress(asList("a", "a", "a", "a", "b", "c", "c", "d", "e", "e", "e", "e")); assertThat(compressedList, hasSize(5)); assertThat(compressedList, contains("a", "b", "c", "d", "e")); }
@Test public void shouldNotRemoveNonConsecutiveSimilarElementsFromAList() throws Exception { List<String> compressedList = P08.compress(asList("a", "a", "a", "a", "b", "c", "c", "a", "a", "d", "e", "e", "e", "e")); assertThat(compressedList, hasSize(6)); assertThat(compressedList, contains("a", "b", "c", "a", "d", "e")); }