Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table like java data structure

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  | +--------+-------+-----+  

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.

like image 308
Sverd Avatar asked Nov 07 '09 01:11

Sverd


People also ask

What data structure is a table?

Table is a data structure which plays a significant role in information retrieval. A set of n distinct records with keys K1, K2, …., Kn are stored in a file. If we want to find a record with a given key value, K, simply access the index given by its key k. The table lookup has a running time of O(1).

What is a table Java?

Guava's Table is a collection that represents a table like structure containing rows, columns and the associated cell values. The row and the column act as an ordered pair of keys. The row and column act as an ordered pair of keys.

Can you make tables in Java?

Java provides a useful class called JTable that enables you to create tables when developing graphical user interfaces using the components of Java's Swing API. You can enable your users to edit the data or just view it.

Which data structure is used in Java?

Types of Data Structures in JavaStack. Queue. Binary Tree. Binary Search Tree.


1 Answers

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.

From the TreeBasedTable docs:

Implementation of Table whose row keys and column keys are ordered by their natural ordering or by supplied comparators.

Example usage:

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] 
like image 147
Andrejs Avatar answered Sep 17 '22 13:09

Andrejs