• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

trouble with LinkedHashMap

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

Hoping someone can see where I messed up. I've created the test class below to which the output is:

id: 40
HID : 1
NAME : sports
id: 40
HID : 4
NAME : sewing
temp.size(): 2
HID/4
NAME/sewing

The final output should be:

HID/1
NAME/sports
HID/4
NAME/sewing

This has me puzzled. temp.size() should be 4 NOT 2. It is clear from the println statements that my nested for loop in method getArrayFieldValues(...) is finding id 40 twice and putting the necessary values into the LinkedHashMap. But by the time it is done, it would seem the first two entries into the hashmap get overwritten or something leaving only the last two entries. I think this is one of those "can't see the forest for the trees" things. I need an extra pair of eyes to show me where I went wrong. Please advise.

Alan

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not going to try to read or understand that code, but whenever the problem is, "There are fewer than there should be, and the originals are getting overwritten," it means you're not creating a new object somewhere that you should be, and the original is being overwritten because you're telling it to.

For example:


Here we have just one Foo object, we create it, set its X to 1, put a reference to it it into the map, set its X to 2, and put another reference to it into the map. Both entries (key="a" and key="b') point to the same Foo object for their value.

A common mistake is to think that map.put() somehow makes a copy of the key and/or value. It doesn't. Just like any method call, it just passes a reference, and the map stores that reference. Objects in Java are not copied unless some code explicitly copies them, such as by calling clone(), or a copy constructor, or System.arraycoyp(). And when you're calling a method, if its docs don't explicitly state that it copies the object, then assume it doesn't.


 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example you provide certainly makes sense, however, in my case, I'm not changing values on any object. A 2D String array is being presented and all the values are fixed. The array contains 2 rows with "40" showing up in the zeroeth element. I'm scanning column 0 for matches to the id passed to the method getArrayFieldValues(...). I'm associating field names with the values in the array's columns 1 and 2 and putting them into the hashmap. My understanding was that everytime you call the put() method on the hashmap, you add a new set of key:value pairs to the map. Which is what I thought I was doing. So I must have written my code in a strange way because the output is not what I expected. I expected 4 separate entries into the hashmap, instead I get 2. I fail to see my mistake. Can you help out further?

Alan
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Shiers wrote:My understanding was that everytime you call the put() method on the hashmap, you add a new set of key:value pairs to the map.



Yes, but you can build a map with 1,000,000 keys and one single value object. That's what I showed in my code, and that's what it sounds like you're doing. Calling put() does not copy any objects or create any new objects. If you call put() twice, and both your get() calls give the same result, even though you think they should be different, then you have merely changed the contents of a single object and re-used it for both put() calls.

You say you're not changing the values in an object. Are you doing something like



?

If so, then you are changing the values in an array object, and both put() calls are putting references to that same array for their values, so any change made to that array will be seen by both keys

If these explanations don't fit what you're doing, then I'm afraid I can't help you, since I don't feel like digging into your code or trying to understand the specifics of your map of lists of whatever.
reply
    Bookmark Topic Watch Topic
  • New Topic