I'm trying to find a way to create a class, SomeNewClass that I can access
like a collection in C# so:
Ideally, hash key pairs would be allowed so:
SomeNewClass snc = new SomeNewClass();
snc["testkey"]["testkey2"] = "testval";
String s = snc["testkey"]["testkey2"];
Chris - 25 Oct 2005 22:30 GMT
> I'm trying to find a way to create a class, SomeNewClass that I can access
> like a collection in C# so:
[quoted text clipped - 5 lines]
>
> String s = snc["testkey"]["testkey2"];
The easy way to do it is to create a concatenated key:
String s = hashMap.get("testkey_testkey2");
unless, of course, you need to get *all* elements associated with "testkey".
In that case, you can create a main hashmap for testkey, and then have the
elements be smaller hashmaps that contain testkey2 elements. The downside to
this approach is that it will be a memory hog, if that matters for your app.
The other downside is that you'd need testkey to be able to access any
testkey2 elements.
Or you could create a sorted hashmap on the concatenated key, and then
iterate over all "testkey" elements to get testkey2. Slower, but would
probably use less memory.
Or abandon hashmaps altogether and just binary search a sorted list of
concatenated keys. Fast, doesn't use much memory, but a lot more work and
not updateable.
Oliver Wong - 25 Oct 2005 22:32 GMT
> I'm trying to find a way to create a class, SomeNewClass that I can access
> like a collection in C# so:
[quoted text clipped - 5 lines]
>
> String s = snc["testkey"]["testkey2"];
The syntax simply doesn't exist in Java. Possible solutions include:
1) Use a getter, e.g. snc.get("testkey").set("testkey2", "testval");
2) Write a new preprocessor language for Java, and the corresponding
preprocessor.
3) Write a new compiler for your extension to the Java language.
- Oliver
Roedy Green - 26 Oct 2005 05:06 GMT
On Tue, 25 Oct 2005 13:57:24 -0700, "Dilton McGowan II"
<diltonm@hotmail.com> wrote, quoted or indirectly quoted someone who
said :
>Ideally, hash key pairs would be allowed so:
>
>SomeNewClass snc = new SomeNewClass();
>snc["testkey"]["testkey2"] = "testval";
>
>String s = snc["testkey"]["testkey2"];
you could build a wrapper around HashMap that glued the keys into one
object in some way, or write your own HashMap that worked with a key
pair and XORED the two hashCodes together.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Dilton McGowan II - 26 Oct 2005 07:09 GMT
> On Tue, 25 Oct 2005 13:57:24 -0700, "Dilton McGowan II"
> <diltonm@hotmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 10 lines]
> object in some way, or write your own HashMap that worked with a key
> pair and XORED the two hashCodes together.
Thanks all, good suggestions.