gpt4 book ai didi

Java - java.lang.IllegalStateException : source already consumed or closed

转载 作者:行者123 更新时间:2023-11-30 07:08:13 26 4
gpt4 key购买 nike

我有一些代码:

    Stream<String> previewImagesURLsList = uploadedVideoItemObj.getPreviewImagesURLsList().parallel();

Stream<HashMap<String, Object>> imagesStream = previewImagesURLsList
.map(new Function<String, HashMap<String, Object>>() {
@Override
public HashMap<String, Object> apply(String fileName) {
HashMap<String, Object> m = new HashMap<>();
m.put("preview_file", fileName);
m.put("parent_id", gotId);
return m;
}
});

HashMap<String, Object>[] filesArr = imagesStream.toArray(HashMap[]::new);

我上线了

 HashMap<String, Object>[] filesArr = imagesStream.toArray(HashMap[]::new);

一个异常(exception)

java.lang.IllegalStateException: source already consumed or closed
at java.util.stream.AbstractPipeline.sourceSpliterator(AbstractPipeline.java:455)
at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:255)
at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:438)
at my.site.objects.videoitems.uploadedVideoItems.dao.impl.JdbcUploadedVideoItemDAO.insert(JdbcUploadedVideoItemDAO.java:79)
at my.site.processors.files.threads.ServerUploadedFileProcessor.processFileServer(ServerUploadedFileProcessor.java:53)
at my.site.processors.files.threads.ServerUploadedFileProcessor.call(ServerUploadedFileProcessor.java:28)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

我看不懂,哪里有bug。这个异常是什么意思,在什么情况下可以抛出?

最佳答案

似乎您的 getPreviewImagesURLsList() 正在返回一个 Stream,尽管它的名称。 Stream 只能使用一次,因此您必须确保每次调用 getPreviewImagesURLsList() 都会返回一个新的 Stream

或者更好的是,让它返回 List,正如它的名字所暗示的那样,让调用者对其调用 stream()

顺便说一下,使用 Stream 的通常方法是链接方法调用而不是将中间结果存储在变量中以避免错误地多次使用这些变量:

HashMap<String, Object>[] filesArr =
uploadedVideoItemObj.getPreviewImagesURLsList().parallel()
// or better if getPreviewImagesURLsList() was returning a List:
// uploadedVideoItemObj.getPreviewImagesURLsList().parallelStream()
.map(fileName -> {
HashMap<String, Object> m = new HashMap<>();
m.put("preview_file", fileName);
m.put("parent_id", gotId);
return m;
}).toArray(HashMap[]::new);

关于Java - java.lang.IllegalStateException : source already consumed or closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24162637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com