• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Problem with Java References

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I need to keep 2 completly different data object structures in sync. I was hoping to do this with a hash map where I keep references to some object variables.

So basically what I need is this:

Data1 d1 = new Data1();
d1.setSomeValue(4.5)

Data1 d2 = new Data2();
d2.setSomeValue(6.7)

HashMap h = new HashMap();
h.put("data1" , d1.SomeValue)
h.get("data1") = d2.getSomeValue

But the SomeValue variable does not get updated in the d1 object instead I just update the value in the HashMap. Is there a way to update the d1 object ?

I know there are better ways to solve this under normal circumstances. But this in a special problem becaus d2 is a tree structure and when something changes in the tree I only get the node id thus I need the hash map approach.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are putting the value of the d1 object in the HashMap. Try putting the d1 object itself in the HashMap instead:
 
Pete Neu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line is exactly the problem:

((Data1) h.get("data1")).setSomeValue(d2.getSomeValue());

The key "data1" should represent a node in a tree. Let's say "child3_2". Now the only chance for mapping the change of this node back to the first data object d1 is the the name "child3_2".

I cannot say call the getter method xy because I don't know which node goes to which value in the data object d1. To make matters worse the first data object has a lot of nested elements

I don't like doing a very long switch loop like this

switch(node_name)
case 1 ; d1.setSomeValue(newNodeValue);

Because this will be a very long list with more loops in it.

Any chance of doing this with reflection or something else?
 
I don't like that guy. The tiny ad agrees with me.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic