按值对番石榴表进行排序 [英] Sorting Guava table on values

查看:74
本文介绍了按值对番石榴表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法做到这一点?预期的效果是rowMap和columnMap条目值将按值排序。

Is there any possible way to do that? The expecting effect would be that rowMap and columnMap entry values would be sorted by value.

问题在于,如果没有Table中的基础映射,我将无法创建比较器。

The problem is that I cannot create a comparator without the underlying maps in Table.

Table table = TreeBasedTable.create(?,?);

Map<String, Map<String, String>> rowMap = table.rowMap();
Map<String, String> thisMapShouldBeSortedByValues = rowMap.get(smth);

Map<String, Map<String, String>> columnMap = table.columnMap();
Map<String, String> thisMapShouldBeSortedByValues = columnMap.get(smth);

现在,我始终必须对rowMap和columnMap进行排序,并在其上分配新的TreeMap。

Now I always have to sort rowMap and columnMap afterwards and allocate new TreeMaps on that.

推荐答案

首先,我假设通过 thisMapShouldBeSortedByValues 表示 thisMapShouldBeSortedByKeys

First of all, I am assuming that by thisMapShouldBeSortedByValues, you mean thisMapShouldBeSortedByKeys

TreeBasedTable扩展了RowSortedTable,因此该表仅按其行而不是其列进行排序。调用 table.columnMap()会得到无序的 Map< C,Map< R,V>

TreeBasedTable extends RowSortedTable, so the table is only sorted by it's rows, not its columns. Calling table.columnMap() gives you an unordered Map<C, Map<R, V>.

由于它是按行排序的,因此 table.rowMap 返回 SortedMap< R,Map< C,V>> 。因此,地图是按行键排序的,但是 Map< C,V> 值是未排序的地图,因此为什么要调用 rowMap.get ()返回无序映射。

Since it is sorted by rows, the table.rowMap returns a SortedMap<R, Map<C, V>>. So the map is sorted on it's row keys, but the Map<C, V> value is an unsorted map, hence why calls to rowMap.get() returns an unordered map.

您可以尝试通过调用table.rowMap()然后再调用rowMap.get()来获取SortedMap,方法是改为调用table.rowKeySet()和table.row(R rowKey)像这样(您通过调用 table.rowMap()来构建您最初想要获取的地图:

The SortedMap you are trying to get from calling table.rowMap(), and then rowMap.get(), can be obtained by instead calling table.rowKeySet() and table.row(R rowKey) like so (You build the map you originally wanted to get by calling table.rowMap():

ImmutableSortedMap.Builder<String, SortedMap<String, String>> builder = ImmutableSortedMap.builder();

for (String rowKey : table.rowKeySet()){
     builder.put(rowKey, table.row(rowKey)) ;
}

 SortedMap<String, <SortedMap<String, String>> thisMapIsSortedByKeys = builder.build();

这篇关于按值对番石榴表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆