将响应添加到Set< string>
Set<String> response = null;
Set<String> success = null;
for (String country : countrys) {
response = service.method(country);
if (response != null) {
success = response;
}
}
这里 service.method
返回一个 Set
。我想将每个循环的响应添加到成功集中。
现在,这段代码只是存储最后一次成功循环的响应。 有人可以尽早帮忙解决这个问题吗?
Set<String> response = null;
Set<String> success = null;
for (String country : countrys) {
response = service.method(country);
if (response != null) {
success = response;
}
}
Here service.method
returns a Set<String>
. I want to add the response of each loop to the success Set.
Now, this code is just storing the response of the last loop in success.
Can someone please help with this at the earliest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
addAll(Collection c)
方法 (参见规范):请记住,您需要将
success
初始化为空集(例如HashSet
) 首先。否则,您将遇到NullPointerException
。You could use the
addAll(Collection<? extends E> c)
method (see spec):Keep in mind that you will want to initialize
success
as an empty set (e.g. aHashSet
) first. Otherwise you will run into aNullPointerException
.