4

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.

Haxor
  • 2,306
  • 4
  • 16
  • 18
  • 1
    Possible duplicate of [Fastest data structure for contains() in Java?](http://stackoverflow.com/questions/3267572/fastest-data-structure-for-contains-in-java) – Yassin Hajaj May 17 '16 at 05:33
  • How often do you need to traverse it? 150*10 is really not a big deal unless you traverse it thousands of times. And in that case you should specify the use case better. – Ingo Bürk May 17 '16 at 05:46
  • You have to describe more - what operations are needed? Array is the best for traversing. But would you need to search? Add (extending limits)? – MBo May 17 '16 at 05:53
  • @IngoBürk - Say it is 10 times in a single program run. – Haxor May 17 '16 at 05:57
  • @MBo - I need to search only. No additions – Haxor May 17 '16 at 05:58
  • 1
    http://stackoverflow.com/questions/716597/array-or-list-in-java-which-is-faster – Zubair Nabi May 17 '16 at 06:29
  • actually a mixture of Alphabets and Integers can be sorted pretty easly – JoulinRouge May 17 '16 at 06:52
  • @Haxor 150 * 10 * 10 = 15000. I wouldn't worry about efficiency too much here. Think of what Knuth said. – Ingo Bürk May 17 '16 at 12:09

3 Answers3

5

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)

MBo
  • 77,366
  • 5
  • 53
  • 86
3

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

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
1

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

Arctigor
  • 247
  • 2
  • 5
  • Note that author needs also `to find a particular value` – MBo May 17 '16 at 06:45
  • yes, and in either way he can find it using array would be like this: array[43][54] and using arraylist would be like this: list.get(43).get(54) – Arctigor May 17 '16 at 06:48
  • 1
    You are writing about getting the value from specific cell. To find if unordered array contains given string, one should walk through **all** C*R cells – MBo May 17 '16 at 06:55
  • yes, you are right, but unfortunately I can't think of a better solution atm, but your solution should be better than mine – Arctigor May 17 '16 at 07:00