In C#, I can transform a single element to an IEnumerable<>
like this (using an extension method here):
static class Extensions
{
static IEnumerable<T> Yield<T>(this T t)
{
yield return t;
}
}
I use this when I need to input an IEnumerable<>
somewhere but only have a single element, like here:
var myList = new List<string>("foo".Yield());
Is there some equivalent way to do this in Java?
I am NOT interested in actually creating an Iterable<>
on the heap from an element like
List<String> myIterable = new ArrayList<String>();
myIterable.add("foo");
because I already know that kind of solution; I want to know if Java can handle enumerables/iterables as powerfully as C# (C# has Linq and yield return, Java has Streams) and lazily.