Collections within Collections within Collections...?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Collections within Collections within Collections...?

  1. #1
    Dan Delaney Guest

    Collections within Collections within Collections...?


    Hello all.

    I'm just now getting into Java programming after spending the past several
    years immersed in PHP and Perl. One of the great things about both of those
    languages is the way they handle Associative Arrays (called "hashes" in Perl).
    For instance, in Perl I could set up a huge data structure out of hashes
    and arrays so that one array could contain a bunch of hashes, and each hash
    could contain other arrays and hashes, and so on. Then to get to a particular
    value, I could just reference it by subscripting through all of them. Thus,
    if I stored a list of hashes containing address information inside a hash
    referenced by names and that was inside an array containing other contact
    lists, I could reference Joe Schmoe's address in the second list with something
    like the following:

    print "Joe's address is " . $AddressLists[1]["SchmoeJoe"]["address"];

    That would get the value of the "address" key in the hash contained in the
    element with the key of "SchmoeJoe" in the hash in element 1 of the @AddressLists
    array.

    I haven't quite figured out how to do something similar in Java using ArrayLists
    and HashMaps. If I were to start an ArrayList called 'addressLists' and put
    in it several HashMaps, each to contain a different address list, then put
    into those HashMaps OTHER HashMaps containing the address information of
    individuals, how would I go about getting the address informatino directly
    out or modifying it directly in a HashMap that is inside a HashMap that inside
    an ArrayList?

    Thanks.
    --Dan

  2. #2
    Kyle Gabhart Guest

    Re: Collections within Collections within Collections...?


    "Dan Delaney" <Dionysos@Dionysia.org> wrote:
    >
    >Hello all.
    >
    >I'm just now getting into Java programming after spending the past several
    >years immersed in PHP and Perl. One of the great things about both of those
    >languages is the way they handle Associative Arrays (called "hashes" in

    Perl).
    >For instance, in Perl I could set up a huge data structure out of hashes
    >and arrays so that one array could contain a bunch of hashes, and each hash
    >could contain other arrays and hashes, and so on. Then to get to a particular
    >value, I could just reference it by subscripting through all of them. Thus,
    >if I stored a list of hashes containing address information inside a hash
    >referenced by names and that was inside an array containing other contact
    >lists, I could reference Joe Schmoe's address in the second list with something
    >like the following:
    >
    >print "Joe's address is " . $AddressLists[1]["SchmoeJoe"]["address"];
    >
    >That would get the value of the "address" key in the hash contained in the
    >element with the key of "SchmoeJoe" in the hash in element 1 of the @AddressLists
    >array.
    >
    >I haven't quite figured out how to do something similar in Java using ArrayLists
    >and HashMaps. If I were to start an ArrayList called 'addressLists' and

    put
    >in it several HashMaps, each to contain a different address list, then put
    >into those HashMaps OTHER HashMaps containing the address information of
    >individuals, how would I go about getting the address informatino directly
    >out or modifying it directly in a HashMap that is inside a HashMap that

    inside
    >an ArrayList?
    >
    >Thanks.
    >--Dan


    Dan,

    In a word -- parentheses. Although it might not be as direct a means of
    accessing nested collections as you are accustomed to, you can access nested
    collections on the same line via appropriately placed parentheses.

    Object obj = ( addressList[1].get("someKey") ).get( "someOther" );

    The above example shows an array of Hashtable/Hashmap objects whose elements
    in turn contain hashes.

    Let me know if I can clarify anything else for you.

    Cordially,

    Kyle Gabhart
    DevX Java Pro

  3. #3
    Dan Delaney Guest

    Re: Collections within Collections within Collections...?


    "Kyle Gabhart" <java@objectsoln.com> wrote:
    >Object obj = ( addressList[1].get("someKey") ).get( "someOther" );


    Cool. Thanks a lot, Kyle! So is the following syntax correct as a reworking
    of the perl example I gave? (This is just a hypothetical example, so I don't
    have a program to test it.)

    PERL:
    print "Joe's address is " . $AddressLists[1]["SchmoeJoe"]["address"];

    JAVA:
    out.println("Joe's address is " +
    ((addressLists.get(1)).get("SchmoeJoe")).get("address") );


    And how about if I want to change Joe's phone number (assuming it's in an
    element called "phone" in the same HashMap as "address"), would the following
    syntax be correct?

    ((addressLists.get(1)).get("SchmoeJoe")).put("phone", "999-999-9999");

    Hmmmm. That doesn't seem quite right--maybe the following?

    ((addressLists.get(1)).put("SchmoeJoe")).put("phone", "999-999-9999");

    No, that doesn't seem quite right either. Hmmmmm. Collections are definitely
    more difficult to get the hang of than Perl hashes. They're pretty cool though.
    They have a lot more functionality.

    Thanks
    --Dan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center