Java – return stream from custom data structure class
I'm developing a program to create a custom LinkedList generic class. I hope to generate a stream from it by calling stream () on the object I checked the documentation and didn't find anything that could help me do this, such as the implemented interface (similar... Iterative, comparative, comparable) I'm thinking about creating a stream method that returns a stream with arrays The stream of stream comes from the array generated by traversing the linked list and allocating elements It doesn't matter. For this generic class, I want its parameters to be constrained to two classes without inheritance A bit like a person or an alien can do this
Solution
I assume that your custom LinkedList class has an iterator () method I also assume that it has a constant time size () method Making these assumptions, you can implement the stream () method using the following code:
public Stream<T> stream() { Iterator<T> it = iterator(); int characteristics = 0; // see characteristics docs in [Spliterator][1] Spliterator<T> spliterator = Spliterators.spliterator(it,size(),charactistics); // see [Spliterators][2] return StreamSupport.stream(spliterator,false); // see [StreamSupport][3] }