P21 { public static <T> List<T> insertAt_split(List<T> list, int pos, T t) throws IllegalArgumentException { if (pos < 1) { throw new IllegalArgumentException("pos can't be less than 1"); } Map<Boolean, List<T>> split = P17.split(list, pos); List<T> first = split.get(true); List<T> second = split.get(false); first.add(pos - 1, t); first.addAll(second); return first; }  static List<T> insertAt(List<T> list, int pos, T t); static List<T> insertAt_split(List<T> list, int pos, T t);  }
@Test public void shouldInsertElementAtSecondPosition_split() throws Exception { List<String> input = Stream.of("a", "b", "c", "d").collect(toList()); List<String> result = P21.insertAt_split(input, 2, "alfa"); assertThat(result, hasSize(5)); assertThat(result, contains("a", "alfa", "b", "c", "d")); }
@Test public void shouldInsertElementAtFirstPosition_split() throws Exception { List<String> input = Stream.of("a", "b", "c", "d").collect(toList()); List<String> result = P21.insertAt_split(input, 1, "alfa"); assertThat(result, hasSize(5)); assertThat(result, contains("alfa", "a", "b", "c", "d")); }
@Test public void shouldInsertElementAtEnd_split() throws Exception { List<String> input = Stream.of("a", "b", "c", "d").collect(toList()); List<String> result = P21.insertAt_split(input, 5, "alfa"); assertThat(result, hasSize(5)); assertThat(result, contains("a", "b", "c", "d", "alfa")); }