### Question:
CollectionUtils { public static <T> T getSingleElement(Iterable<T> iterable) { Iterator<T> iterator = iterable.iterator(); Assert.isTrue(iterator.hasNext(), () -> "Expected one element but got zero"); T value = iterator.next(); Assert.isFalse(iterator.hasNext(), () -> "Expected one element but got at least two"); return value; } private  CollectionUtils(); static T getSingleElement(Iterable<T> iterable); static T getLastElement(List<T> list); static T getSingleElement(Iterable<T> iterable, Supplier<? extends RuntimeException> exceptionSupplier); static T removeAll(T map, Collection<K> keysToRemove);  }### Answer:
@Test void testGetSingleElement() throws Exception { assertThat(CollectionUtils.getSingleElement(Collections.singletonList(1))).isEqualTo(1); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> CollectionUtils.getSingleElement(Collections.emptyList())) .withMessage("Expected one element but got zero"); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> CollectionUtils.getSingleElement(Arrays.asList(1, 2))) .withMessage("Expected one element but got at least two"); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> CollectionUtils.getSingleElement(Arrays.asList(1, 2, 3))) .withMessage("Expected one element but got at least two"); }
@Test void testGetSingleElement_exceptionSupplier() throws Exception { Supplier<RuntimeException> exceptionSupplier = () -> new MongoServerException("too many elements"); assertThat(CollectionUtils.getSingleElement(Collections.singletonList(1), exceptionSupplier)).isEqualTo(1); assertThatExceptionOfType(MongoServerException.class) .isThrownBy(() -> CollectionUtils.getSingleElement(Collections.emptyList(), exceptionSupplier)) .withMessage("too many elements"); assertThatExceptionOfType(MongoServerException.class) .isThrownBy(() -> CollectionUtils.getSingleElement(Arrays.asList(1, 2), exceptionSupplier)) .withMessage("too many elements"); }