命名返回流的java方法 [英] Naming java methods that return streams

查看:405
本文介绍了命名返回流的java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有返回Stream的方法的命名约定?
我发现的唯一提及是这个关于SO的答案(最后一段),但我看不出是什么它是基于。

Is there naming convention for methods that return Stream? The only mention I found is this answer on S.O (last paragraph), but I don't see what is it based on.

推荐答案

自从我写了那段以来,我觉得有必要回答。 : - )

Since I wrote that paragraph I feel compelled to answer. :-)

假设您有一个表示单个类型事物聚合的类,并且您想要返回 Stream 他们给来电者。

Suppose you have a class that represents an aggregation of things of a single type, and you want to return a Stream of them to the caller.

如果你完全明确地告诉你什么,你也可以调用方法流()。 JDK中有很多方法,名为 stream(),它们返回一个明显类型的流。

If it's totally unambiguous as to what you're returning, you might just as well call the method stream(). There are a lot of methods in the JDK named stream() that return a stream of the obvious type.

有时您返回的是对同一事物或不同事物或其他事物的不同表示。在这种情况下,似乎有一个约定,选择一个复数名词,表示在流中返回的事物的类型。

Sometimes what you're returning is different representations of the same thing, or different kinds of things, or whatever. In that case there does seem to be a convention to choose a plural noun that represents the type of things being returned in the stream.

要查看这些,请查看Javadoc然后点击顶部导航栏中的使用链接。这将带您进入交叉引用页面。查找具有您感兴趣类型的返回值的所有方法。

To see these, look in the Javadoc and click the Use link in the top navigation bar. This will take you to a cross-reference page. Look for all methods that have return values of the type you're interested in.

例如,请参阅 href的使用页面=http://docs.oracle.com/javase/8/docs/api/java/util/stream/class-use/Stream.html\"rel =noreferrer> Stream IntStream LongStream DoubleStream 。有许多返回流的名为 stream()的方法。但也有:

For example, see the Use pages for Stream, IntStream, LongStream, and DoubleStream. There are lots of methods named stream() that return streams. But there are also:


  • java.io.BufferedReader.lines()

  • java.lang.CharSequence.chars()

  • java .lang.CharSequence.codePoints()

  • java.nio.CharBuffer.chars()

  • java.nio.file.File.lines()

  • java.util.Random.ints()

  • java.util.Random.longs()

  • java.util.Random.doubles()

  • java .util.SplittableRandom.ints()

  • java.util.SplittableRandom.longs()

  • java.util.SplittableRandom.doubles()

  • java。 util.concurrent.ThreadLocalRandom.ints()

  • java.util.concurrent.ThreadLocalRandom.longs()

  • java.util.concurrent.ThreadLocalRandom.doubles()

  • java.io.BufferedReader.lines()
  • java.lang.CharSequence.chars()
  • java.lang.CharSequence.codePoints()
  • java.nio.CharBuffer.chars()
  • java.nio.file.File.lines()
  • java.util.Random.ints()
  • java.util.Random.longs()
  • java.util.Random.doubles()
  • java.util.SplittableRandom.ints()
  • java.util.SplittableRandom.longs()
  • java.util.SplittableRandom.doubles()
  • java.util.concurrent.ThreadLocalRandom.ints()
  • java.util.concurrent.ThreadLocalRandom.longs()
  • java.util.concurrent.ThreadLocalRandom.doubles()

当然,有很多方法都不符合这一点。 NIO文件实用程序类具有 Files.find() Files.list() Files.walk()。分割字符串的结果流由 java.util.regex.Pattern.splitAsStream 返回。我认为没有人喜欢 AsStream 后缀,但是再一次,没有人能想到更好的东西。另一方面,建议的JDK 9增强以获得正则表达式匹配结果流将被命名为 Matcher.results()

Of course, there are a lot of methods that don't conform to this. The NIO file utility class has Files.find(), Files.list(), and Files.walk(). A stream of results from splitting a string is returned by java.util.regex.Pattern.splitAsStream. I don't think anybody likes the AsStream suffix but then again, nobody could think of anything better. On the other hand, a proposed JDK 9 enhancement to get a stream of regex match results will be named Matcher.results().

这篇关于命名返回流的java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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