Java流已关闭。

huangapple go评论145阅读模式
英文:

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&lt;Map.Entry&lt;String,Integer&gt;&gt; sorted =
    				    map.entrySet().stream()
    				       .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
    			
    			Supplier&lt;Stream&lt;Map.Entry&lt;String,Integer&gt;&gt;&gt;  sort2 = () -&gt; sorted;
    			Optional&lt;String&gt; kk = Optional.of(sort2.get().findFirst().get().getKey());
    			Optional&lt;Integer&gt; vv = Optional.of(sort2.get().findFirst().get().getValue());
    			int vmax = vv.get().intValue() ;	
    			int count=0;
    			ArrayList&lt;String&gt; a = new ArrayList&lt;String&gt;() ;
    			for(Map.Entry&lt;String,Integer&gt; h: map.entrySet() ) {
    				if(h.getValue()==vmax) {
    					a.add(h.getKey()) ;
    					count++;
    				}
    			}
    			
    			if(count&gt;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&lt;Stream&lt;Map.Entry&lt;String,Integer&gt;&gt;&gt; sort2 = () -&gt;  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&lt;Map.Entry&lt;String, Integer&gt;&gt; firstEntry = sorted.findFirst();
Optional&lt;String&gt; kk = firstEntry.map(Map.Entry::getKey);
Optional&lt;Integer&gt; 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&lt;Map.Entry&lt;String, Integer&gt;&gt; maxEntry = map.entrySet().stream()
    .max(Map.Entry.comparingByValue());

huangapple
  • 本文由 发表于 2020年8月19日 10:59:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63479371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定