Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

tree - Java Sort a Guava TreeBasedTable

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should recreate TreeBasedTable and supply to it another Comparator. Please see the guava documentation. Also note, that this internally uses trees that must be sorted with proper comparator (when you are building the tree, comparator is known, so that tree is built in specific way). Supplying other comparator would result in building new tree. If this isn't desired behaviour for you, you should be thinking about using other data structure.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...