Possible Duplicate:
Table like java data structure
I am looking for a Table-like data structure for Java. So far I have not been able to find one and it is really frustrating as I know for sure there must be one. Please note this has nothing to do with a graphical representation of the table, just the logical model.
The idea is simple, you have a table like:
+-------+-----+-----------+
| Name | Age | city |
+-------+-----+-----------+
| Steve | 21 | London |
| John | 40 | Amsterdam |
+-------+-----+-----------+
Now I need to iterate over the different rows and retrieve the values based on the column name. It has to be flexible so I don’t know the column names up front (so a list of beans is not an option). The best structure I could find that has this ability is a java.sql.ResultSet, but I don’t feel comfortable using that in a non SQL related context. I also don’t want a list of HashMaps as that would mean storing the keys for every row, which would be very inefficient.
It seems like such a basic structure yet I cannot find anything about it due to the overwhelming amount of GUI related posts when searching for table like structures in Java.
EDIT
For a concrete example of what I need, the following piece of code:
TableStructure table = getTable(); // Gets my desired table with its filling
while (table.next()){
System.out.println("Name: " + table.get("Name"));
System.out.println("Age: " + table.get("Age"));
System.out.println("City: " + table.get("City"));
System.out.println("NonExistent: " + table.get("NonExistent"));
}
Should produce the following output:
Name: Steve
Age: 21
City: London
NonExistent: null
Name: John
Age: 40
City: Amsterdam
NonExistent: null
A
ResultSetis a list (or a set, dependant on necessity of ordering and multiplicity of elements) of tuples, so a really generic answer would be(or better, have
TableStructureextendListand use the iterator directly)where
This construct allows for different row sizes within your table, which in a DB setting violates the first (or the second?) normal form principle.
Hope that helps a bit.
Cheers,