File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Which Collection/Data Structure? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Which Collection/Data Structure?" Watch "Which Collection/Data Structure?" New topic
Author

Which Collection/Data Structure?

Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
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


    Normal is in the eye of the beholder
    Thomas Paul
    mister krabs
    Ranch Hand

    Joined: May 05, 2000
    Posts: 13974
    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.


    Associate Instructor - Hofstra University
    Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
    Leslie Chaim
    Ranch Hand

    Joined: May 22, 2002
    Posts: 336
    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

    Joined: Sep 27, 2002
    Posts: 51
    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 ]

    SJCP 1.2
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    FWIW: Here's my Collections Crib Sheet adapted from a Sun newsletter with hints to help you choose which collection class to use.


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    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?


    "I'm not back." - Bill Harding, Twister
    Leslie Chaim
    Ranch Hand

    Joined: May 22, 2002
    Posts: 336
    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

    Joined: Jan 30, 2000
    Posts: 18671
    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

    Joined: May 22, 2002
    Posts: 336
    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Which Collection/Data Structure?