我应该暴露Stream< T>在我的界面上? [英] Should I be exposing Stream<T> on my interface?

查看:155
本文介绍了我应该暴露Stream< T>在我的界面上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正在编写.NET代码,我会经常公开 IEnumerable< T> 哪里可能有意义。也许这取决于LINQ以及你可以使用foreach的事实,但它感觉做对了。

If I am writing .NET code I would often expose IEnumerable<T> where-ever it could possibly make sense. Maybe it was down to LINQ and the fact you could use foreach, but it felt "right" to do it.

private List<String> _myList;

public IEnumerable<String> MyList
{
    get { return _myList; }
}

现在我正在编写Java 8代码,我正在辩论我是否应该以可能的方式公开 Stream< T> ?也许是因为我必须打电话给 Collection.stream(),感觉就像是正在工作,但感觉不对?

Now I am writing Java 8 code and I am debating whether I should be exposing Stream<T> where possible in the same way? Maybe its because I have to call Collection.stream() and it feels like it is "doing work", but it doesn't feel right?

private Collection<String> myCollection;

public Stream<String> getStuff() {
    return myCollection.stream();
}

流< T> 意味着以与 IEnumerable< T> 可能相同的方式暴露在界面上?

Is Stream<T> meant to be exposed on an interface in the same way that IEnumerable<T> might be?

推荐答案

你问的是错误的问题。毕竟,支持两者并不困难,例如。

You are asking the wrong question. After all, it isn’t hard to support both, e.g.

Collection<Foo> getStuff();
default Stream<Foo> stuff() {
    return getStuff().stream();
}

因此使用您的界面的代码不需要显式 stream()调用,而接口的实现者也不需要打扰它。

so code using your interface doesn’t need an explicit stream() call, while implementors of the interface don’t need to bother with it as well.

因为你总是通过 Collection.stream()或明确地公开 Stream 支持,问题是你是否要公开集合 。虽然为集合后端提供是很便宜的,但收集一个后端可能会很昂贵收集来自

As you are always exposing a Stream support whether via Collection.stream() or explicitly, the question is whether you want to expose the Collection. While it is cheap to provide a Stream for a Collection back-end it might turn out to be expensive to collect a Collection from a Stream.

所以界面暴露两种方式都表明它们同样可用,而对于不使用 Collection 后端的实现,这些方法中的一种可能比另一种方法更昂贵。

So an interface exposing both ways suggests that they are equally usable while for an implementation not using a Collection back-end one of these methods might be way more expensive than the other.

因此,如果您确定所有实施(包括未来的实施)将始终使用(或必须支持)集合,那么它可能是通过API公开它有用,因为集合支持 Stream 的某些操作。如果您支持通过公开的集合修改基础数据,则尤其如此。

So if you are sure that all implementations, including future ones, will always use (or have to support) a Collection it might be useful to expose it though the API as Collections support certain operations which Stream doesn’t. That’s especially true if you support modification of the underlying data via the exposed Collection.

否则,支持访问可能是更好的选择。这使得实现可以自由地拥有其他后端而不是 Collection 。但是,这也意味着此API不支持Java 8之前的Java版本。

Otherwise, supporting Stream access only might be the better choice. This gives implementations the freedom to have other back-ends than a Collection. However, that also implies that this API does not support Java versions prior to Java 8.

这篇关于我应该暴露Stream&lt; T&gt;在我的界面上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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