Java 8扩展流< T> [英] Java 8 extending stream<T>

查看:113
本文介绍了Java 8扩展流< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展Java 8的Stream实现.

I'm attempting to extend Java 8's Stream implementation.

我有这个界面:

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

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

在我的主要方法中:

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

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

我正在尝试将Stream强制转换为我的界面StreamStuff,并使用我的方法.

I'm trying to cast the Stream, to my interface StreamStuff, and use my method.

出现以下错误:

线程"main"中的异常java.lang.ClassCastException:java.util.stream.IntPipeline $ Head无法转换为StreamStuff

Exception in thread "main" java.lang.ClassCastException: java.util.stream.IntPipeline$Head cannot be cast to StreamStuff

当我这样做时会遇到相同的错误

I get the same error when I do:

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

我想知道这种事情是否可能,如果可以,我如何实现呢?供参考,我有点使用这篇文章作为指南.

I'm wondering if this sort of thing is even possible and if so, how do I achieve this? For reference I'm kind of using this article as a guide.

推荐答案

如上所述,您可以创建自己的包装器实现:

As already mentioned, you can create your own wrapper implementation:

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扩展StreamWrapper.这样一来,您只能在StreamStuff中使用自定义方法,而不能使用其他流方法.您还可以在StreamWrapper中将所有被覆盖的方法都设置为final,以避免意外覆盖它们.

You will have to delegate all methods from the interface, so the class will be pretty big. You might consider adding a class StreamWrapper which will delegate all methods from the interface and then have your actual class StreamStuff extend StreamWrapper. This would allow you to have only your custom methods in StreamStuff and no other stream methods. You might also make all overridden methods in StreamWrapper final to avoid accidentally overriding them.

然后您可以像这样使用它:

Then you can use it like this:

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);
}

自定义方法返回一个新的包装器,因此您可以将调用链接到自定义方法.

The custom method returns a new wrapper so you can chain calls to your custom methods.

请注意,转换为Double的对象可能会抛出ClassCastException,因此您可以考虑将普通的T替换为Double,从而将委托流限制为该特定类型.

Just note that your cast to Double might throw ClassCastException so you might consider replacing generic T with Double, hence limiting the delegate stream to be of that specific type.

这篇关于Java 8扩展流&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆