GeeCON Prague 2014*
The moose likes Java in General and the fly likes directly reference objects via TreeMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "directly reference objects via TreeMap" Watch "directly reference objects via TreeMap" New topic
Author

directly reference objects via TreeMap

Gabe Herbert
Greenhorn

Joined: Apr 28, 2005
Posts: 10
(I've reversed the order of my paragraphs to put the important stuff first. If my question doesn't make sense, reading further back in time may clarify)


The big question I have is wether there's a way to use a reference (ie objArray[0].v1 = 42) directly when using a map. I've unly came across:

as my way of actually "using" the objects referenced by my map. How can I directly use an object referenced by a TreeMap?

I'm working with a bunch of dynamically-created objects, and this has worked fine so far. I would, however, prefer to use a TreeMap instead of an array, so I can just iterate through the objects that exist, rather than iterating through thousands of array cells waiting to stumble on the ones I'm actually using.

To my understanding, I just created an array of TestObj references, referenced a new object via objArray[0], and returned the value of the v1 variable of the object referenced by objArray[0].



Consider the following:
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
I don't understand what the problem is.
There is nothing magic about a reference obtained from a TreeMap, use it like any other reference. Once you have done the "get" and cast the reference to the appropriate type, use it like any other reference to that type.
Bill
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You can, of course, put all in one statement, just as with an array:



Why do you ask? You aren't micro-optimizing, are you?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Gabe Herbert:
I would, however, prefer to use a TreeMap instead of an array, so I can just iterate through the objects that exist, rather than iterating through thousands of array cells waiting to stumble on the ones I'm actually using.


BTW, I've no idea what you are talking about here. Care to elaborate?
Gabe Herbert
Greenhorn

Joined: Apr 28, 2005
Posts: 10
Thank you, Ilja Preuss! Despite confusing you and everyone else with my lack of concision, you perfectly answered my question with
((TestObj)objMap.get(new Integer(1))).v1 = 43;

This is my first experiment with collections and I thought it didn't work because I forgot to put the whole thing in parentheses *blush*.

I'll work on the whole "talkin too much and sayin too little" thing...

Gabe

I'm not micro-optimizing, I'm just obnoxiously anal.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I would, however, prefer to use a TreeMap instead of an array, so I can just iterate through the objects that exist, rather than iterating through thousands of array cells waiting to stumble on the ones I'm actually using.


This bit made sense to me. In a real array you'd allocate 0..max slots in memory but only use a very few. You'd have to iterate every slot to find the used ones. You can key a map by the index, put in only the items you are using and iterate them very quickly with the keyset or entryset. Back in the early days of spreadsheets the "sparse array" that doesn't really allocate all the slots was a nifty new trick. It might well work for you here.

I would probably try both in a small experiment and compare times to see if the map is any faster. To try my hand at premature optimization I might experiment with eliminating the new Integer() from all the accesses, too.

Let us know what you find!!


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
Gabe Herbert
Greenhorn

Joined: Apr 28, 2005
Posts: 10
To try my hand at premature optimization I might experiment with eliminating the new Integer() from all the accesses, too.


I don't understand what you mean here. I'm guessing that you're talking about autoboxing, but that's just a guess. Could you please elucidate?

Gabe
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:

This bit made sense to me. In a real array you'd allocate 0..max slots in memory but only use a very few. You'd have to iterate every slot to find the used ones. You can key a map by the index, put in only the items you are using and iterate them very quickly with the keyset or entryset.


Now I get it!

Thanks Stan!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: directly reference objects via TreeMap