Hi Paul,
I see the point, that making it too easy there. I might have to some
little explain where I have started from. I wanted to go over all
resource URL's from a ClassLoader and read them as a Stream:
Enumeration<URL> resources =
myInstance.getClassLoader().getResources("resource.name");
Collections.list(resources).stream()....
Which will internally copy all elements right away into an ArrayList and
does more than I wanted... :-)
So, with that your good reasons in mind, in my case leaded me in a wrong
solution in the first attempt too - thanks good I looked into the source
code ;-)
In my case then is the used approach to get a Stream correct?
Cheers
Patrick
On 14.06.2016 18:26, Paul Sandoz wrote:
> Hi Patrick,
>
> Enumeration now has an asIterator method, that’s our attempt to bridge the
> old traversal world to the less old ( :-) ) world.
>
> Bridging the gap between the less old world and the new world (streams) is
> more subtle, and we have been holding out on that.
>
> We wanted to avoid providing simple methods to create a Stream from an
> Iterator, that might serve as an attractive nuisance e.g.
> Stream.of(arrayList.iterator()), rather than arrayList.stream(), and there
> are details of size, order etc for which any defaults might be poor or just
> wrong depending the Iterator’s source of elements.
>
> For example, your helper method assumes that the Iterator is covering a
> structure that is both immutable and ordered, but what if the Iterator was
> obtained from a HashSet?
>
> If we chose to provide such a helper method we would have to assume that the
> Iterator’s source of elements is of unknown size, order and mutability,
> essentially no Spliterator characteristics can be derived from the Iterator.
>
> Paul.
>
>> On 14 Jun 2016, at 01:13, Patrick Reinhart <[email protected]> wrote:
>>
>> Hi there,
>>
>> I know you are busy getting the latest release ready. Still I have question
>> according the java.util.stream.Stream's static helper methods. During my
>> work I ran into a couple places where creating a Stream out of a Iterator or
>> Enumeration directly would be quite handy.
>>
>> At the moment I have made me two methods like this on a helper class in oder
>> to not duplicate the code:
>>
>> public static <T> Stream<T> iterate(Iterator<T> iterator) {
>> Objects.requireNonNull(iterator);
>> return StreamSupport.stream(Spliterators
>> .spliteratorUnknownSize(iterator, Spliterator.ORDERED |
>> Spliterator.IMMUTABLE), false);
>> }
>>
>> public static <T> Stream<T> iterate(Enumeration<T> enumeration) {
>> Objects.requireNonNull(enumeration);
>> final Iterator<T> iterator = new Iterator<T>() {
>> @Override
>> public boolean hasNext() {
>> return enumeration.hasMoreElements();
>> }
>>
>> @Override
>> public T next() {
>> return enumeration.nextElement();
>> }
>> };
>> return iterate(iterator);
>> }
>>
>>
>> My question is now, if it would be worth while having something like this on
>> the Stream itself?
>>
>>
>> Cheers
>>
>> Patrick