Java 8 Extending Stream <T>

I am trying to extend the implementation of Java 8 Stream.

I have this interface:

public interface StreamStuff<T> extends Stream<T> {

    Stream<T> delegate();
    default Stream<T> biggerThanFour() {
        return delegate().filter(i -> ((Double)i > 4));
    }
}

And in my main method:

int arr [] = {1,2,3,4,5,6};

Object array [] = ((StreamStuff)Arrays
            .stream(arr))
            .biggerThanFour()
            .toArray();

I am trying to include Stream in my StreamStuff interface and use my method.

Im getting the following error:

An exception in the stream "main" java.lang.ClassCastException: java.util.stream.IntPipeline $ Head cannot be passed to StreamStuff

I get the same error as me:

StreamStuff ss = (StreamStuff)Arrays.stream(arr);

I wonder if this is possible, and if so, how do I achieve this? For reference, I kind of use this article as a guide.

+4
source share
3 answers

, :

public class MyStream<T> implements Stream<T> {

    private final Stream<T> delegate;

    public MyStream(Stream<T> delegate) {
        this.delegate = delegate;
    }

    @Override
    public Stream<T> filter(Predicate<? super T> predicate) {
        return delegate.filter(predicate);
    }

    @Override
    public void forEach(Consumer<? super T> action) {
        delegate.forEach(action);
    }

    MyStream<T> biggerThanFour() {
        return new MyStream<>(delegate.filter(i -> ((Double) i > 4)));
    }

    // all other methods from the interface
}

, . , StreamWrapper, , StreamStuff extend StreamWrapper. StreamStuff . StreamWrapper final, .

:

public static void main(String[] args) {
    Stream<Double> orgStream = Stream.of(1.0, 3.0, 7.0, 2.0, 9.0);

    MyStream<Double> myStream = new MyStream<>(orgStream);

    myStream.biggerThanFour().forEach(System.out::println);
}

, .

, Double ClassCastException, generic T Double, .

+4

stream() Arrays, Stream - . Stream , , - . - :

int[] filtered = new StreamStuff(Arrays.stream(arr)).biggerThanFour().toArray();

, , ?

int[] filtered = Arrays.stream(arr).filter(i -> i > 4).toArray();
+5

, Stream<T> , -:

    public interface MyStream<T> {

    Stream<T> stream();

    static <T> MyStream<T> of(Stream<T> stream) {
        return () -> stream;
    }

    default <U> MyStream<U> stream(Function<Stream<T>, Stream<U>> stream) {
        return of(stream.apply(stream()));
    }

    //Watch out with Double cast. Check the type in method or restrict it via generic
    default MyStream<T> biggerThanFour() {
        return of(stream().filter(i -> ((Double) i > 4)));
    }

    //Watch out with Double cast. Check the type in method or restrict it via generic
    //Another method
    default MyStream<T> biggerThanFourteen() {
        return of(stream().filter(i -> ((Double) i > 14)));
    }
}

, stream() , , of(...), stream(...), Function<T,U> Stream , , , biggerThanFour(). , , Stream<T> ( , Stream<T> ) .
, , , , :

List<Integer> doubles = MyStream.of(Stream.of(1.0, 3.0, 7.0, 2.0, 9.0)) // create instance
                .biggerThanFour() //call MyStream methods
                .stream(doubleStream -> doubleStream.map(aDouble -> aDouble * 2)) //Do youre base stream intermediate methods and return again MyStream so you can call more specific custom methods
                .biggerThanFourteen()
                .stream() // call the base stream more or less your delegate for last intermediate methods and terminal method
                .mapToInt(Double::intValue)
                .boxed() //Ah if you have IntStreams and similar you can call the boxed() method to get an equivalent stream method.
                .collect(Collectors.toList()); // terminal method call

, [18];)

+1

Source: https://habr.com/ru/post/1657404/


All Articles