Reputation: 1794
I am using Java 8 streams
API to transform values of map to array. I am getting following exception:
Unhandled exception ScheduledExecutorService : java.lang.IllegalStateException: End size 84758 is less than fixed size 84764
at java.util.stream.Nodes$FixedNodeBuilder.end(Nodes.java:1232)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:545)
at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:438)
at com.turnoutnow.businesslogic.Worker.generateVisitsToESAndS3(Worker.java:213)
I am searching the exception on Google, but not finding anything useful around the error. Could anyone please tell me exactly what could be the reason for this error?
EDIT
Here is the single line of code where its coming:
visits.getVisitDataMap().values().stream().toArray(n -> new VisitDataBE[n]);
Upvotes: 3
Views: 2145
Reputation: 44942
Your current code is correct as you are creating the array of the right size. You can simplify it to .toArray(VisitDataBE[]::new)
.
Most likely other thread is removing elements from visits.getVisitDataMap()
map while the toArray()
is running. You can test this hypothesis by creating a defensive copy of the Map
:
Map<...> copy = new HashMap<>(visits.getVisitDataMap());
copy.values().stream().toArray(n -> new VisitDataBE[n]);
The proper fix would depend on your application multi-threaded semantics.
Upvotes: 3