Java对番石榴树进行排序

编程入门 行业动态 更新时间:2024-10-23 16:23:18
本文介绍了Java对番石榴树进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

我尝试的方法是遍历表,并为每个值更改在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对番石榴树进行排序

本文发布于:2023-10-15 02:47:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1493020.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:番石榴   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!