Haxor
Haxor

Reputation: 2306

What is the best data structure to implement a 2D Matrix with String data type?

I have huge table data with 150 rows and 10 Columns, each column has String data. After storing the data, I have to traverse as well to find a particular value. So, I am looking for answers to the best data structure in this case in terms of performance, flexibility of traversing.

I have thought of Array, ArrayList, Hashmap.

Also, I have found similar questions on SO but they don't answer my question.

EDIT: The data is a mixture of Alphabets and Integers. Cannot be sorted and contains duplicates as well.

Upvotes: 4

Views: 4666

Answers (3)

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27476

Guava has a Table structure that looks like you could use, it has containsValue(...) method to find particular value, and you can also traverse it.

Here's a general explanation of the Table:

Typically, when you are trying to index on more than one key at a time, you will wind up with something like Map<FirstName, Map<LastName, Person>>, which is ugly and awkward to use. Guava provides a new collection type, Table, which supports this use case for any "row" type and "column" type.

You would be most probably interested in following implementation of the Table interface:

ArrayTable, which requires that the complete universe of rows and columns be specified at construction time, but is backed by a two-dimensional array to improve speed and memory efficiency when the table is dense. ArrayTable works somewhat differently from other implementations

Upvotes: 3

Arctigor
Arctigor

Reputation: 247

just in this case, I would use a String[][] because you can access the elements with a complexity of O(1)

but as I said, only in this case. If the number of the rows or columns is dynamically modified then I'd use List<List<String>>, more exactly ArrayList

Upvotes: 1

MBo
MBo

Reputation: 80187

It seems then for such table size combination 2D Array[][] + Hashmap would be an excellent choice. Simple and effective.

Array contains values and allows to traverse the table in any order.

HashMap contains pairs <String; TPoint> (coordinates in array - Row/Col pair).

If you need only to know whether the table contains some string, then don't store coordinates in Map.

I think that Guava Table proposed by @krzyk, provides similar functionality (don't know about performance)

Upvotes: 5

Related Questions