英文:
java stream is closed
问题
// 代码
Stream<Map.Entry<String, Integer>> sorted =
map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
Supplier<Stream<Map.Entry<String, Integer>>> sort2 = () -> sorted;
Optional<String> kk = Optional.of(sort2.get().findFirst().get().getKey());
Optional<Integer> vv = Optional.of(sort2.get().findFirst().get().getValue());
int vmax = vv.get().intValue();
int count = 0;
ArrayList<String> a = new ArrayList<String>();
for (Map.Entry<String, Integer> h : map.entrySet()) {
if (h.getValue() == vmax) {
a.add(h.getKey());
count++;
}
}
if (count > 1) {
Collections.sort(a);
System.out.println(a.get(0));
} else {
System.out.println(kk.get());
}
map.clear();
英文:
I want to use stream getfirst method two times but an error occurred says (java.lang.IllegalStateException: stream has already been operated upon or closed) and this stream code begins at comment named here.
//code
Stream<Map.Entry<String,Integer>> sorted =
map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
Supplier<Stream<Map.Entry<String,Integer>>> sort2 = () -> sorted;
Optional<String> kk = Optional.of(sort2.get().findFirst().get().getKey());
Optional<Integer> vv = Optional.of(sort2.get().findFirst().get().getValue());
int vmax = vv.get().intValue() ;
int count=0;
ArrayList<String> a = new ArrayList<String>() ;
for(Map.Entry<String,Integer> h: map.entrySet() ) {
if(h.getValue()==vmax) {
a.add(h.getKey()) ;
count++;
}
}
if(count>1) {
Collections.sort(a);
System.out.println(a.get(0));
}
else {
System.out.println(kk.get());
}
map.clear();
}
}
}
答案1
得分: 1
你不能再次使用流。创建一个变量来保存 findFirst()
的值。
英文:
You can't use a stream twice. Create a variable to hold the value of findFirst()
答案2
得分: 0
sort2
对你没有任何作用,只是隐藏了一个问题,即你无法重复使用 sorted
。
在这种情况下,到目前为止最好的解决方案是将 sorted.findFirst()
存储在一个变量中并重复使用它,而不是两次调用 findFirst()
。如果你想这样做,那么你需要编写:
Supplier<Stream<Map.Entry<String, Integer>>> sort2 = () -> map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
...这将对条目进行两次排序,这将非常低效。
英文:
sort2
is not doing anything for you, and simply hiding the issue that you can't reuse sorted
.
In this situation, by far the best solution is to store sorted.findFirst()
in a variable and reuse it rather than call findFirst()
twice. If you wanted to do it like this, then you would have to write
Supplier<Stream<Map.Entry<String,Integer>>> sort2 = () -> map.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
...which would sort the entries twice, which would be very inefficient.
答案3
得分: 0
Stream.findFirst
是一个终端操作 - 像你现在所做的那样在同一个流上调用findFirst
两次是不可能的。
看起来你不需要调用两次:即使你重新启动了一个新的流,两次都会期望得到相同的映射条目。所以只需要调用一次findFirst
,并使用它返回给你的Optional:
Optional<Map.Entry<String, Integer>> firstEntry = sorted.findFirst();
Optional<String> kk = firstEntry.map(Map.Entry::getKey);
Optional<Integer> vv = firstEntry.map(Map.Entry::getValue);
注意,有一种比排序更好的方法可以从流中获取最大的条目:使用max
操作符。
Optional<Map.Entry<String, Integer>> maxEntry = map.entrySet().stream()
.max(Map.Entry.comparingByValue());
英文:
Stream.findFirst
is a terminal operation - calling findFirst
twice on the same stream like you are doing is not possible.
It looks like you don't need to call it twice: even if you restarted a new stream, both times you would expect to get the same map entry. So call findFirst
only once, and use the Optional it returns to you:
Optional<Map.Entry<String, Integer>> firstEntry = sorted.findFirst();
Optional<String> kk = firstEntry.map(Map.Entry::getKey);
Optional<Integer> vv = firstEntry.map(Map.Entry::getValue);
Note that there's a better way to get the maximum item from the stream than sorting: using the max
operator.
Optional<Map.Entry<String, Integer>> maxEntry = map.entrySet().stream()
.max(Map.Entry.comparingByValue());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论