Is there a general best approach for var-arg method signatures with respect to the Java 8 Lambda/Closure?
In a situation where a method is used either with a single object as a parameter or a collection of objects I see three options to realize this:
private void optionOneVararg(String... params) { ... }
private void optionTwoCollection(Collection<String> params) { ... }
private void optionThreeStream(Stream<String> params) { ... }
The String class is obviously used as an example.
Is there a general valid "best practise" approach to this? The vararg approach produced the slickest code up to Java 7. With streams it feels clumsy.
I would like to be able to use the convenience of lambdas both inside the method and outside where I call it. Also I would like to keep wrapping/gluecode (e.g. Collections.singletonList(element)
or Arrays.asList(element)
) to a minimum and avoid using Collection myCollection = stream.collect(...)
followed by a myCollection.stream()
.