public static <T> Collector<T, ?, Optional<T>> singleton() {
        return Collectors.collectingAndThen(
                Collectors.toList(),
                list -> {
                    if (list.size() > 1) throw new IllegalArgumentException("More than one element");
                    return list.stream().findAny();
                }
        );
    }
@Test
    public void collector_returns_singleton() {
        List<String> items = List.of("foo1", "bar", "foo2");
        Optional<String> bar = items.stream().filter(s -> s.startsWith("bar")).collect(CustomCollectors.singleton());
        assertEquals(Optional.of("bar"), bar);
    }