• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which Collection/Data Structure?

 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading in a file of records, as I am reading I build a List for all, given the requirements below what would be the Collection of choice.
My requirements are as follows:
  • Grows on demand
  • Nodes are never deleted
  • Ability (while reading the file) to lookup a node via a given property (from the node) and then add some more properties to the found node.
  • Return an Object[] array after all finishing reading the file


  • Thanks in advance,
    Leslie
     
    Thomas Paul
    mister krabs
    Ranch Hand
    Posts: 13974
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Critical points:
    1) random access by a property
    2) no random insertion or removal
    Sounds like a HashMap would do the job. Use the lookup property as the key.
     
    Leslie Chaim
    Ranch Hand
    Posts: 336
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Will HashMap maintain the order of insertion , once I am done reading the records I need to convert it to an Object[]
    Maybe an :roll: OrderderdHashMap :roll:
     
    peter greaves
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    in which case java.util.LinkedHashMap (which extends HashMap)is the class you need - from the API
    "This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order)"
    regs
    peter
    [ June 16, 2003: Message edited by: peter greaves ]
     
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    FWIW: Here's my Collections Crib Sheet adapted from a Sun newsletter with hints to help you choose which collection class to use.
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ability (while reading the file) to lookup a node via a given property (from the node) and then add some more properties to the found node.
    The fact that you add more properties rather than replace something makes me think you might actually need a LinkedHashMap with an ArrayList for each value, where each ArrayList contains multiple properties (Strings?) associated with that key/node. Or perhaps each value should be a Properties object or even a LinkedHashMap? (Since you do call these things "properties" and you may wish to continue using insertion order even within a given node.) I'm just guessing - it depends on what sort of data you're dealing with, and how it's organized. Are there multiple key-value relationships pertaining to a single node?
     
    Leslie Chaim
    Ranch Hand
    Posts: 336
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First of all, welcome back Jim! I hope you enjoyed your vacation . Nine days with no posting to the ranch that must be a record for you
    Yes, you're right about your assumptions as an ideal OO zealot. However, in my case a node is just some class that I created (let's call it SegmentUnit). A SegmentUnit has several (String) properties which are read from a file.
    The file is structured as follows, first all A properties for all SegmentUnit(s) then all B properties, and there could be any number of SegmentUnit(s).
    I could have approach it with your way. Then iterate over the LinkedHashMap to create an array of SegmentUnit (which is what I ultimately need). However, it's simpler to keep the SegmentUnit(s) as the values in the LinkedHashMap and then just get the SegmentUnit array in insertion order.
    BTW, what's the best way to create this array?
    Oh, and one more thing, I have this 3-page word document which I wrote, which addressed a question that I had. As I wrote it up I came up with the right solution, should I post it anyway, the thing is > 5000 chars.
    Cheers,
    Leslie
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First of all, welcome back Jim! I hope you enjoyed your vacation.
    Thank you, thank you.
    Nine days with no posting to the ranch that must be a record for you.
    Probably. I logged in a few times, just long enough to delete my daily spam and verify that the ranch hadn't burned down.
    BTW, what's the best way to create this array?
    You mean, after you've built up all the SegmentUnit objects, which are currently stored as values in a Map, you now want to ditch the Map and just get an array of the values in the order they were inserted? Ummm...
    SegmentUnit[] array = (SegmentUnit[]) map.values().toArray(new SegmentUnit[0]);
    Everything should still be in insertion order.
    Oh, and one more thing, I have this 3-page word document which I wrote, which addressed a question that I had. As I wrote it up I came up with the right solution, should I post it anyway, the thing is > 5000 chars.
    Hard to say - depends on the problem and its solution. If you're happy with your solution, then probably not, unless it's something general-interest enough that others will benefit from seeing it. Or unless you've just got a really cool solution you want to show off. But if you think there might also be other approaches you haven't considered but would like to hear about - post away. Though I may just gloss over it myself; I'm still busy playing catch-up. Cheers...
    [ June 18, 2003: Message edited by: Jim Yingst ]
     
    Leslie Chaim
    Ranch Hand
    Posts: 336
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    unless it's something general-interest enough that others will benefit from seeing it.
    Well I would like to think that it is, but for that we need to ask them
    But if you think there might also be other approaches you haven't considered but would like to hear about - post away.
    Sure, here you go!
    Thanks Jim!
    [ June 19, 2003: Message edited by: Leslie Chaim ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic