表如java数据结构 [英] Table like java data structure
问题描述
我需要实现一些类似的数据结构,可以在Java中存储这样的信息:
I need to implement some kind table-like data structure that stores info like this in Java:
+--------+-------+-----+
| sij | i | j |
+--------+-------+-----+
| 45 | 5 | 7 |
+--------+-------+-----+
| 33 | 1 | 6 |
+--------+-------+-----+
| 31 | 0 | 9 |
+--------+-------+-----+
| 12 | 8 | 2 |
+--------+-------+-----+
,我必须能够通过 sij
参数对表进行排序。我已经使用 ArrayList
和 HashMap
进行了一些测试,但是我无法使它们工作得很好。 >
and I have to be able to sort the table by the sij
parameter. I've made some tests with ArrayList
and HashMap
, but I can't make them work well.
推荐答案
有一个通用的 TreeBasedTable
课程 guava 库,它完全符合你所要求的。它还提供了许多其他有用的实用程序方法,其用法显示在用户指南
There is a generic TreeBasedTable
class from Google guava library which does exactly what you are asking for. It also offers many other useful utility methods and its usage is shown in the user guide.
从 TreeBasedTable
文档:
执行表的行按键和列键按其自然排序或提供的比较器排序。
Implementation of Table whose row keys and column keys are ordered by their natural ordering or by supplied comparators.
示例用法:
RowSortedTable<Vertex, Vertex, Double> weightedGraph = TreeBasedTable.create();
weightedGraph.put(v2, v3, 4.0);
weightedGraph.put(v1, v2, 20.0);
System.out.println( weightedGraph.rowKeySet() ); // prints [v1, v2]
这篇关于表如java数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!