• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

directly reference objects via TreeMap

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic