将 ImmutableListMultimap 与 GWT 一起使用时,我无法通过列表索引访问值。我能够迭代值列表,但尝试按索引访问值会引发索引越界异常。我正在使用 ListMultimap 来保留插入顺序。
ListMultimap<String,String> testMultimap =
ImmutableListMultimap.<String,String>builder()
.putAll("key1","value1","value2")
.putAll("key2","value3","value4")
.build();
//this works to get the values correctly, but it's a terrible hack
List<String> values = testMultimap.get("key1");
String firstValue = "";
String secondValue = "";
int i=0;
for(String val : values){
if(i==0){firstValue=val;}
else {secondValue = val;}
i++;
}
//throws index out of bound
firstValue = testMultimap.get("key1").get(0);
部分堆栈跟踪:
java.lang.IndexOutOfBoundsException: Index: 0
at java.util.Collections$EmptyList.get(Collections.java:3204)
at com.google.common.collect.ForwardingImmutableList.get(ForwardingImmutableList.java:46)
at com.google.corp.gwt.cell.Cells$3.render(Cells.java:114)
at com.google.corp.gwt.cell.ForwardingCell.render(ForwardingCell.java:62)
at com.google.corp.gwt.cell.TableCompositeCell$TableDataCell.render(TableCompositeCell.java:302)
at com.google.corp.gwt.cell.TableCompositeCell$TableRowCell.render(TableCompositeCell.java:341)
at com.google.gwt.cell.client.CompositeCell.render(CompositeCell.java:151)
at com.google.corp.gwt.cell.TableCompositeCell$TableRowCell.render(TableCompositeCell.java:347)
at com.google.corp.gwt.cell.TableCompositeCell.render(TableCompositeCell.java:91)
....
at com.google.gwt.cell.client.CompositeCell.render(CompositeCell.java:151) at com.google.corp.gwt.cell.TableCompositeCell.render(TableCompositeCell.java:115) at com.google.gwt.user.cellview.client.Column.render(Column.java:165) at com.google.gwt.user.cellview.client.AbstractCellTableBuilder.renderCell(AbstractCellTableBuilder.java:206) at com.google.gwt.user.cellview.client.DefaultCellTableBuilder.buildRowImpl(DefaultCellTableBuilder.java:129) at com.google.gwt.user.cellview.client.AbstractCellTableBuilder.buildRow(AbstractCellTableBuilder.java:87) at com.google.gwt.user.cellview.client.AbstractCellTable.buildRowValues(AbstractCellTable.java:2357) at com.google.gwt.user.cellview.client.AbstractCellTable.replaceAllChildren(AbstractCellTable.java:2158) at com.google.gwt.user.cellview.client.AbstractHasData$View.replaceAllChildren(AbstractHasData.java:245) at com.google.gwt.user.cellview.client.HasDataPresenter.resolvePendingState(HasDataPresenter.java:1351) at com.google.gwt.user.cellview.client.HasDataPresenter.access$3(HasDataPresenter.java:1062) at com.google.gwt.user.cellview.client.HasDataPresenter$2.execute(HasDataPresenter.java:984) at com.google.gwt.core.client.impl.SchedulerImpl$Task$.executeScheduled$(SchedulerImpl.java:50) at com.google.gwt.core.client.impl.SchedulerImpl.runScheduledTasks(SchedulerImpl.java:180) at com.google.gwt.core.client.impl.SchedulerImpl.flushFinallyCommands(SchedulerImpl.java:278) at com.google.gwt.core.client.impl.Impl.exit(Impl.java:296) at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:287) at sun.reflect.GeneratedMethodAccessor267.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103) at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71) at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172) at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293) at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547) at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364) at java.lang.Thread.run(Thread.java:722)