类似于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
类来自 Google 番石榴 库,它完全符合您的要求.它还提供了许多其他有用的实用方法,其用法显示在用户指南.
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屋!