Java对番石榴树进行排序 [英] Java Sort a Guava TreeBasedTable

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

问题描述

我有一个来自Guava的TreeBasedTable对象(Gooogle集合).它采用TreeBasedTable<k1, k2, v>的形式. k1k2实现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.

我尝试的方法是遍历表,并为每个值更改在Compare方法中使用的变量.麻烦在于它一次通过循环,然后返回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方法将其复制到其中,该怎么办?每个元素的放置权将为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对番石榴树进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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