Results 1 to 9 of 9
Like Tree3Likes
  • 2 Post By Norm
  • 1 Post By Tolls

Thread: What data structure should I use?

  1. #1
    toffoo is offline Member
    Join Date
    Dec 2014
    Posts
    1
    Rep Power
    0

    Default What data structure should I use?

    If i have many sets of items, where each item can be in more than one set, and I want to remove an item from every set it is in, what data structure would be best for me to use? I guess i could just iterate through each set looking for the item that needs to be removed but there has to be a better way. I was hoping to have some kind of link between the item and the sets the item is in so they can be removed easily. Anyone have any suggestions for me?
    Thanks for any help.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: What data structure should I use?

    Whether you are using a List or Set implementation, the easiest way is to iterate over a collection of sets or lists and just remove the object. Note: Don't iterate over each set or list looking for the object as it is not necessary. If you have a List named myList with a bunch of objects, then myList.remove(obj) will remove the first obj it encounters. If the list has more than one, then you need to repeat it. The remove method returns true if the object existed and false if it didn't. Sets, by their very nature, may not contain duplicates.

    Regards,
    Jim
    Last edited by jim829; 12-07-2014 at 05:19 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: What data structure should I use?

    link between the item and the sets the item is in
    Have a list in the item of the sets it is in.
    jim829 and gimbal2 like this.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: What data structure should I use?

    Quote Originally Posted by Norm View Post
    Have a list in the item of the sets it is in.
    I like that idea especially because you can have an addToSet method which takes the target set as an argument. The method can then record the set and add the item to the same set.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: What data structure should I use?

    I don't know why, but I feel Bertrand Russell's paradox coming up ... maybe you can have a map of sets, where your T type key is associated with a set that contains sets which contain the T type key.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: What data structure should I use?

    Quote Originally Posted by Norm View Post
    Have a list in the item of the sets it is in.
    That is a good idea, although I would then still maintain that meta data externally. IMO its better to have one class that takes care of storing/hiding both the actual collection and the meta data of the objects stored within that collection and keep the application objects completely detached from it all - then you have also only one class responsible for making sure the collection and meta data stay synced.

    Perhaps that is what Jos is trying to explain too, his words are like the wind to me ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: What data structure should I use?

    Quote Originally Posted by gimbal2 View Post
    Perhaps that is what Jos is trying to explain too, his words are like the wind to me ;)
    His words frequently remind me of wind.
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: What data structure should I use?

    My eloquent phrases often flabbergast you plebeians, so there! :-P

    kindest regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: What data structure should I use?

    I am rubber, you are glue!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. List data structure
    By suhaas.mohandos@gmail.com in forum New To Java
    Replies: 4
    Last Post: 08-14-2013, 09:10 AM
  2. Query about data structure
    By chkontog in forum New To Java
    Replies: 1
    Last Post: 11-15-2012, 07:26 PM
  3. Semantic data structure
    By dacoolest in forum Advanced Java
    Replies: 6
    Last Post: 01-12-2012, 06:25 PM
  4. Which data structure to use?
    By malaguena in forum New To Java
    Replies: 4
    Last Post: 04-05-2011, 04:41 PM
  5. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 05:48 AM

Posting Permissions

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