Results 1 to 5 of 5
  1. #1
    oontvoo is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default What is the most efficient data structure for storing possibly upto thousands of ...

    What is the most efficient data structure for storing possibly up to thousands of elements?

    I'm reading from a text file (line by line) and I need store each line into memory. So what data structure should I use?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by oontvoo View Post
    What is the most efficient data structure for storing possibly up to thousands of elements?

    I'm reading from a text file (line by line) and I need store each line into memory. So what data structure should I use?
    It depends what you want to do with those lines; if you just want to process them one by one in the order they were read there is no need to store them all, otherwise a List of some form comes to mind, but, again, it depends on how you want to traverse through them and what you want to do with them.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    oontvoo is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    Well, I'd basically need to constantly traverse the "list" and remove items as needed. So I was thinking about using a Vector from the java.util package simply because there are predefined methods that could help me do that.
    So I guess the question, is it OK to use a vector to store up to thousands of elements?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You could use Vector or ArrayList (which is preferred of the two) as long as you don't run out of memory, but if you need to store thousands of Strings, also consider using a database.

  5. #5
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Generally speaking, you should not use Vector; use ArrayList instead. Vector is thread safe, which makes it slow, and you don't normally need a thread safe collection.

    If the data fits in memory, I would suggest using 'java.util.LinkedList', due to your 'need to constantly traverse the "list" and remove items as needed.' Use the Lists' iterator() method and the remove() method on the Iterator. (Using 'Iterator.remove()' avoids ConcurrentModificationException.) LinkedList is much faster at adding and removing elements in the middle of the list. But beware: Methods using 'int index' are slow.

Similar Threads

  1. Which data structure to use?
    By malaguena in forum New To Java
    Replies: 4
    Last Post: 04-05-2011, 05:41 PM
  2. Spliting Large String Data (upto 2 GB)
    By piyush@java in forum Advanced Java
    Replies: 5
    Last Post: 01-12-2011, 03:23 PM
  3. Storing/retrieving arrays in a data structure...
    By pbandjay in forum New To Java
    Replies: 0
    Last Post: 11-03-2010, 01:39 AM
  4. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 06:48 AM
  5. Queue data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •