File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

directly reference objects via TreeMap

 
Gabe Herbert
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 13047
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Gabe Herbert
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic