I have a use case in which I must return a collection with at least one element. An incoming collection may contain 0 or more elements.
therefore, it can be done quite easily.
Set<ObjectB> setOfB = collectionOfA.isEmpty() ? new HashSet<ObjectB>() {{ add(new ObjectB()); }} : collectionOfA .stream() .map(item -> new ObjectB(item)) .collect(Collectors.toSet());
BUT...
I am also trying to use this as an opportunity to get better acquainted with the tools and functions of Java 8, and therefore I am trying to understand whether this can be done without a conditional test and in more Java 8- as a way.
Thoughts and suggestions are welcome!
source share