与有条件的流有关hashmap的迭代
因此,我现在有这个循环
for (Map.Entry<String, Integer> val : map.entrySet()) {
if (val.getValue() >= 10 && !Objects.equals(val.getKey(), " ") && val.getKey().length() > 3) {
stats = stats + val.getKey().toLowerCase() + " - " + val.getValue() + "\n";
}
}
,我想摆脱循环和使用流的条件。我该怎么做?
So I have this loop
for (Map.Entry<String, Integer> val : map.entrySet()) {
if (val.getValue() >= 10 && !Objects.equals(val.getKey(), " ") && val.getKey().length() > 3) {
stats = stats + val.getKey().toLowerCase() + " - " + val.getValue() + "\n";
}
}
Now I want to get rid of for loop and if condition using streams. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能是一种方法:
Here could be an approach:
有很多方法,这是一种。
然后,我将定义A
谓词&lt; string&lt; string,Integer&gt;&gt;
以减少流构造的杂物。请注意,由于您正在检查长度&lt; 3
可以省略单个空间的条件。如果您是要检查空白字符串,则可以使用!val.getKey()。isblank()
作为条件。降低
采用初始参数,然后是binaryoperator
作为累加器。在这种情况下,积累是映射字符串的串联。String :: Concat
符合要求,并在此处用于完成操作。然后,这只是流式传输
entryset
的问题。打印以下内容。
There are many approaches, here is one.
Then I would define a
Predicate<Entry<String, Integer>>
to reduce the clutter of the stream construct. Note that since you are checking forlength < 3
the condition for a single space can be omitted. If you meant check for a blank String, you can use!val.getKey().isBlank()
as the condition.reduce
takes an initial argument followed by aBinaryOperator
as an accumulator. In this case the accumulation is a concatenation of the mapped strings.String::concat
meets the requirements and is used here to fulfill the operation.Then it is just a matter of streaming the
EntrySet
.Prints the following.
要充分利用流功能,您需要使用流,过滤和减少功能如下:
过滤器在流上应用的方法:
降低在流对象上功能:
To have a full use of stream functionalities You need to use stream, filter and reduce functions as follow:
The stream method applied to a Collection give a new Stream of the item present in the set:
The filter method appplied on the stream:
The reduce function on the stream object:
使用foreach方法的好工作。最好使用流和流API。
这是一个非常简单的程序,可以做类似于您想做的事情。
现在,我们可以编译并运行代码。
现在,您的代码中有什么错误?
错误是这样。 MAP.FOREACH方法采用双调节器。双调查器操作采用两个输入参数(K和V),并且返回没有结果。您正在使用双调查器操作来返回三元表达式中的结果。双调节器操作具有返回类型的void类型。因此,您可以做的是...您可以执行双项量操作内部的代码(就像我在示例中一样),但是您无法返回双调解器操作中的结果(您正在尝试执行此操作)。
您可以在Java文档中阅读有关MAP.FORECH方法和Biconsumer功能操作的信息。
Good job using the forEach method. It's good practice to use streams and the stream API.
Here's a very simple program that does something similar to what you want to do.
Now we can compile and run the code.
Now, what's the error in your code?
The error is this. The Map.forEach method takes a BiConsumer. The BiConsumer operation takes two input arguments (k and v) and returns no result. You are using a BiConsumer operation to return a result in a ternary expression. The BiConsumer operation has a return type of void. So what you can do is... you can execute code inside of the BiConsumer operation (like I did in my example) but you cannot return a result in the BiConsumer operation (which you are trying to do).
You can read about the Map.forEach method and the BiConsumer functional operation in the Java docs.