Results 1 to 5 of 5
  1. #1
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default Which data structure to use?

    Hello,

    I need a data structure which:

    1) I will be able to remove an item (without knowing the index of that item)
    2) I can draw an item randomly (normally in an array, I generate a random number and draw that item in that index.. data structures that use keys -hashtables, hashmaps vs need an iterator to go through the items)
    3) I will be able to add an item (the adding order does not matter)

    Which data structure gives faster solutions?

    Thanks..

  2. #2
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    HashSet can do all of those things. For #2, you have to use the toArray() method, though.

  3. #3
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Good idea. Have you got any idea about the performance of converting a HashSet to an array?

    Thanks for the reply.

  4. #4
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    I'm sorry, I don't know the answer to that. I'm guessing it would be O(N), since you have to move the elements one by one. But I'm not sure.

  5. #5
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Yes, it should depend on the size of the HashSet, I guess.
    Thanks again.

Similar Threads

  1. Which data structure to choose ?
    By lumpy in forum New To Java
    Replies: 2
    Last Post: 02-20-2010, 04:43 AM
  2. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 05:48 AM
  3. data file structure
    By Nicholas Jordan in forum Advanced Java
    Replies: 2
    Last Post: 01-07-2009, 04:16 AM
  4. Queue data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:35 PM
  5. data structure code
    By vgvt in forum New To Java
    Replies: 1
    Last Post: 01-17-2008, 02:49 PM

Tags for this Thread

Posting Permissions

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