我有一个TreeBasedTable<String,String,CustomType>结构,我需要能够从中获取基于start和end范围索引的子集,fromindex例如toindex. 该cellSet方法不返回SortedSet. 最好的方法是什么?
我想过做Lists.newArrayList(structure.cellSet()).subList(start,end),但看起来不像是一件有效的事情。
如果startindex和endindex是整数位置,那么您的ArrayList实现实际上与可行的最佳实现相差不远,尽管编写起来会更有效
FluentIterable.from(table.cellSet()).skip(fromIndex).limit(toIndex).toList()
该实现不会将任何更多的元素复制到结果列表中。
一般来说,对于 Java 附带的任意 、 或几乎任何已排序的数据结构,都没有有效的方法来执行此SortedSet操作SortedMap。
使用 时TreeBasedTable, 的实现rowMap()实际上返回一个SortedMap.
所以你应该使用:
@SuppressWarnings("unchecked") // safe cast because TreeBasedTable returns SortedMap
final SortedMap<String, Map<String, CustomType>> rowMap = (SortedMap<String, Map<String, CustomType>>) myTable.rowMap();
final SortedMap<String, Map<String, CustomType>> subRowMap = rowMap.subMap(start, end);
所以start并且end会像为 a 工作rowMap一样subList(start,end)工作List。