gpt4 book ai didi

java - 使用 Java 8 Stream 的 DataProvider 查询中的 IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-30 06:40:01 24 4
gpt4 key购买 nike

我想从后端延迟加载过滤后的数据。如果我应用从后端返回 1 个元素(或未应用过滤器)的过滤器,一切正常。

如果获取的项目超过 1,它开始崩溃/抛出 OutOfBoundsException。我使用了 official Vaadin documentation作为我的实现指南。

异常是在 Stream-API 的 .filter() 方法中抛出的。我也知道异常的含义,尝试访问值的索引 >= 列表的最大大小,但我不确定如何解决这个问题。将第一个查询的限制设置为 limit-1 并没有解决问题。

我认为回调中的第二个查询有问题,但我无法弄清楚问题出在哪里。是 Stream-API 问题还是第二个回调查询没有正常工作?

public DataProvider<T, String> createLazyLoadingProvider(List<T> data) {
DataProvider<T, String> lazyLoadingProvider
= DataProvider.fromFilteringCallbacks(query -> {
// First Query
final int offset = query.getOffset();
final int limit = query.getLimit();
final String filter = query.getFilter().orElse("");

return data
.stream()
.skip(offset)
.limit(limit)
.filter(e -> {
return e.toString().startsWith(filter);
});


}, query -> {
// Second query
return (int) data.stream()
.filter(e -> {
return e.toString().startsWith(filter);
})
.count();
});
return lazyLoadingProvider;
}

产生:

First Query
Offset: 0
Limit: 100
Callback-Filter: 3
Full Size of Data: 6005
From backend fetched Items: 5
-----------
Second Query
Offset: 0
Limit: 2147483647
Callback-Filter: 3
Max fetchable Items with filter applied: 381

堆栈跟踪:

java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 5 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:458) at com.vaadin.flow.data.provider.DataCommunicator.lambda$getJsonItems$3(DataCommunicator.java:611) at java.base/java.util.stream.IntPipeline$1$1.accept(IntPipeline.java:180) at java.base/java.util.stream.Streams$RangeIntSpliterator.forEachRemaining(Streams.java:104) at java.base/java.util.Spliterator$OfInt.forEachRemaining(Spliterator.java:699) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) at com.vaadin.flow.data.provider.DataCommunicator.getJsonItems(DataCommunicator.java:613) at com.vaadin.flow.data.provider.DataCommunicator.collectChangesToSend(DataCommunicator.java:556) at com.vaadin.flow.data.provider.DataCommunicator.flush(DataCommunicator.java:473) at com.vaadin.flow.data.provider.DataCommunicator.lambda$requestFlush$2f364bb9$1(DataCommunicator.java:421) at com.vaadin.flow.internal.StateTree.lambda$runExecutionsBeforeClientResponse$1(StateTree.java:364) at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) at com.vaadin.flow.internal.StateTree.runExecutionsBeforeClientResponse(StateTree.java:361) at com.vaadin.flow.server.communication.UidlWriter.encodeChanges(UidlWriter.java:392) at com.vaadin.flow.server.communication.UidlWriter.createUidl(UidlWriter.java:182) at com.vaadin.flow.server.communication.UidlRequestHandler.writeUidl(UidlRequestHandler.java:116) at com.vaadin.flow.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:89) at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40) at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1540) at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:246) at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:760) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1617) at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:226) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1604) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:545) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:536) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:235) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1592) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1296) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:485) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1562) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1211) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:221) at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146) at org.eclipse.jetty.server.handler.gzip.GzipHandler.handle(GzipHandler.java:717) at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) at org.eclipse.jetty.server.Server.handle(Server.java:500) at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:386) at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:562) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:378) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:270) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103) at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.onFillable(SslConnection.java:543) at org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:398) at org.eclipse.jetty.io.ssl.SslConnection$2.succeeded(SslConnection.java:161) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103) at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:117) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:336) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:313) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:171) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:129) at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:388) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:806) at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:938) at java.base/java.lang.Thread.run(Thread.java:834)

最佳答案

在应用偏移量和限制之前,您需要过滤

阐述:

假设您有项目 Apple、Cucumber、Lemon、Lettuce、Linseed、Lime、Tangerine、Tomato,并且希望过滤器仅包含以 L 开头的项目。

您的计数查询返回计数 4(Lemon、Lettuce、Linseed、Lime)。

现在假设网格需要前两项,所以它发送偏移量 0 和限制 2。

您的代码所做的是选择前两个项目,Apple、Cucumber,然后过滤它们,并返回 0 个项目。

它需要做的是过滤项目以获得 Lemon, Lettuce, Linseed, Lime,然后取偏移量 0 和限制 2 以获得 Lemon, Lettuce

关于java - 使用 Java 8 Stream 的 DataProvider 查询中的 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59046992/

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