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 <patr...@reini.net> 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

Reply via email to