Java 对 Guava TreeBasedTable 进行排序 [英] Java Sort a Guava TreeBasedTable
问题描述
我有一个来自 Guava (Gooogle Collections) 的 TreeBasedTable
对象.它的形式是TreeBasedTable
.k1
和 k2
实现了 Comparable
.现在,当我显示表格时,我希望用户能够以不同的方式对其进行排序以更改值的顺序.
I have a TreeBasedTable
object from Guava (Gooogle Collections). It is in the form of TreeBasedTable<k1, k2, v>
. k1
and k2
implement Comparable
. Now when I display the table, I want the user to be able to sort it different ways to change the order of the values.
我尝试的方法是遍历表,并为每个值更改比较方法中使用的变量.问题是它通过循环一次,然后返回一个 NullPointerException
,我想我已经缩小范围,尝试在迭代时更改顺序(新手错误,我知道).
The approach I tried was to iterate over the table, and for each value, change a variable that's used in the Compare method. The trouble is it gets through the loop once, and then returns a NullPointerException
, which I think I've narrowed down to trying to change the order whilst iterating over it (rookie mistake, I know).
所以我想知道重新排序这张表的好方法是什么?
So I was wondering, what would be a good way to reorder this table?
推荐答案
如果你只是创建一个新的 TreeBasedTable,使用不同的比较器对象创建,然后通过 public putAll 方法将原始复制到其中会怎样?每个元素的 put 将花费 log(N),所以总时间应该是 N log(n),这大约是我期望重新排序花费的时间.
How about if you just create a new TreeBasedTable, created with a different comparator object, and then copy the original into it via public putAll method? The put for each element will take log(N), so total time should be N log(n), which is about the time I would expect for a re-sort to take.
这篇关于Java 对 Guava TreeBasedTable 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!