user1538814
user1538814

Reputation: 211

best way to store a list of strings

What would be the best way to store and read a really long string, with each entry is an index for another array?

Right now I have this

String indices="1,4,6,19,22,54,....."

The string has up to hundred of thousand entries, so I think maybe I could use a data structure like Linked List. Does anyone know if it would be faster to use one?

Upvotes: 4

Views: 23886

Answers (4)

Abhishek Garg
Abhishek Garg

Reputation: 3242

I have similar hunch in my mind , in which I want to like 1k number of strings and parse them (searching purpose to know it contain item or not).

Hence I found instead of using java collection framework - map or set or list

if I store data simply in array and start parsing data using for-loop, it is faster.

You visit this link and see actual output which we calculated in micro seconds.

https://www.programcreek.com/2014/04/check-if-array-contains-a-value-java/

So using simple brute force is winner in case of unsorted array (normally we have).

But arrays.BinarySearch() is winner if array is sorted.

Upvotes: 1

Hoon
Hoon

Reputation: 411

I really like using the ArrayList class, which if your comfortable using arrays, ArrayList or any member of the Collections Framework. Would work really well. For what your trying to do.

ArrayList<String> indices = new ArrayList<String>();
indices.add("");

Upvotes: 1

Filipe Fedalto
Filipe Fedalto

Reputation: 2540

It would depend on what you'll do with the string (the indices) and the corresponding arrays. Also, it will depend on how you're gonna access them.

I'd suggest you first read an overview about the data structures implemented in java, specially in the Collections Framework.

We could give some suggestions, but you'd have to provide us more information, specially those I mentioned in the beginning (what you want, how this data will be stored and accessed, and so on).

For example, if you need to have a fast access to the indexed data, maybe a string isn't even the best approach. Maybe a map would be better. The indexes could be the keys and the indexed arrays could be the values of the map, for example. But this is just a void example, I strongly suggest you give us more information.

Upvotes: 2

PSR
PSR

Reputation: 40318

List<String> list = new ArrayList<String>();

list.add("1");
list.add("2");

you need to declare arraylist of type string.Then add to it.

Upvotes: 4

Related Questions