英文:
Is it possible use current stream functions inside stream function?
问题
例如,我有一些字符串。我想通过空格将它分割,然后筛选只有长度大于流计数的单词。请参考下面的代码:
Arrays.stream("一些重要的字符串".split(" "))
.filter(word -> word.length() > /*(如何在此获取流计数?)*/)
任务是在一行内完成它(不使用外部变量)。
英文:
For example I have some string. I want split it by space and then filter only words that have length that is greater than stream count. See code below
Arrays.stream("Some important string".split(" "))
.filter(word->word.length>([HOW GET HERE STREAM COUNT??]))
The task is do it in one line (without external variables)
答案1
得分: 1
这是一个hack,我强烈不建议使用,但你可以像这样做:
String[] result = Stream.<String[]>of("Some important strings to test".split(" "))
.flatMap(words -> Stream.of(words).filter(word -> word.length() > words.length))
.toArray(String[]::new);
System.out.println(Arrays.toString(result));
如果你只是简单地使用一个变量,代码会好得多:
String[] words = "Some important strings to test".split(" ");
String[] result = Stream.of(words)
.filter(word -> word.length() > words.length)
.toArray(String[]::new);
System.out.println(Arrays.toString(result));
两者的输出
[important, strings]
如果问题不是关于如何使用split()
返回的数组,而实际上是字面上的问题**“如何获取流的数量”**,那么答案是你无法做到。
即使你可以访问流,也无法调用 count()
方法,因为那会消耗流并违反调用的目的。
英文:
This is a hack, and I would strongly discourage the use, but you can do it like this:
String[] result = Stream.<String[]>of("Some important strings to test".split(" "))
.flatMap(words -> Stream.of(words).filter(word -> word.length() > words.length))
.toArray(String[]::new);
System.out.println(Arrays.toString(result));
The code would be much better if you simply use a variable:
String[] words = "Some important strings to test".split(" ");
String[] result = Stream.of(words)
.filter(word -> word.length() > words.length)
.toArray(String[]::new);
System.out.println(Arrays.toString(result));
Output from both
[important, strings]
If the question is not about how to do it with an array returned by split()
, but is actually the literal question "how to get stream count", then the answer is that you can't.
Even if you had access to the stream, you couldn't call count()
, since that would consume the stream and defy the purpose of the call.
答案2
得分: 0
这不重要。
流只能被迭代一次,因此如果在流上调用.count()
,就不能再调用.filter()
。
下面是一个使用count()
在表达式中的笨拙示例,用于对其进行filter()
:
Stream.of(Arrays.stream("一些重要的字符串".split(" ")))
.map(s -> s.filter(x -> x.length() > s.count())).findAny().get().toArray();
这是可预测的结果:
Exception in thread "main" java.lang.IllegalStateException:
流已经被操作或关闭
在 java.util.stream.AbstractPipeline.<init>(AbstractPipeline.java:203)
英文:
It doesn't matter.
A stream can only be iterated over once, so if you call .count()
on the stream you can no longer call .filter()
.
Here's a kludgy example that uses count()
on the stream in the expression that filter()
s it:
Stream.of(Arrays.stream("Some important string".split(" ")))
.map(s -> s.filter(x -> x.length() > s.count())).findAny().get().toArray();
And here's the predictable result:
Exception in thread "main" java.lang.IllegalStateException:
stream has already been operated upon or closed
at java.util.stream.AbstractPipeline.<init>(AbstractPipeline.java:203)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论