Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java collector to Optional<T>

Using Java streams and generics I’m trying to create a Collector, that would return me Optional.empty, if stream is empty, Optional<T> if stream has one item and throw exception if stream has more than one item.

public static <T, R extends Optional<T>> Collector<T, ?, R> toOptional() {
    return Collectors.collectingAndThen(
            Collectors.toList(),
            list -> {
                if (list.size() > 1) {
                    throw new CollectingException("More than one item found in: " + list);
                }
                if (list.size() == 1) {
                    return Optional.of(list.get(0));
                }
                return Optional.empty();
            }
    );
}

However I’m getting an error I don’t understand.

Required type:
Collector<T,?,R>
Provided:
Collector<Object,Object,Object>
no instance(s) of type variable(s) T exist so that Optional<T> conforms to R inference variable RR has incompatible bounds: equality constraints: R lower bounds: Optional<T1868063>

The desired usage would be

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

List<Person> people;
Optional<Person> john = people.stream().filter(person->"John".equals(person.getName())).collect(toOptional());

Could anyone please explain me what’s wrong?

>Solution :

The declaration of your method is not correct, instead of:

public static <T, R extends Optional<T>> Collector<T, ?, R> toOptional() {

You can just use:

public static <T> Collector<T, ?, Optional<T>> toOptional() {

In other words, the method need to return an Optional object of type T directly, instead of using a separate type parameter R that extends Optional<T>.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading