• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Treemap replace method using two maps with missing values

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I would like to replace key values of map1 with values of map2 EXCEPT for keys with "na". Below is my code, it outputs {k1=v1, k2=na, k4=v4} instead of {k1=v11, k2=na, k4=v44} in map1. The second entry value should not change. Please help me troubleshoot. Thanks
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Don't use the == and != operators to compare String values. Use the equals method instead:



 
glen viet
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Paul Clapham, corrected, thanks. Apparently that is not the only issue
 
Paul Clapham
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's curious that your replaceValues method doesn't use map2 anywhere.
 
Paul Clapham
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And did you intend to use TreeMap's "replace(K key, V oldValue, V newValue)" method? The variable names you used in your code suggest you did that wrong.
 
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
map1.replace(k,k, v);
doesn't do what you think it does
try
map1.replace(k,v);
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, wrong again.
'v' has to come from map2.
 
glen viet
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Paul Clapham && Carey Brown; Thanks for your inputs.


//Tried several combinations; of even passing inputs, seems! replace() cannot handle keys individually
//The close it can get with updated code order produced:
//{k1=v11, k2=v22, k4=v44}
//Was expecting instead!!
//{k1=v11, k2=na, k4=v44}
 
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't edit the original post to incorporate corrections from later. I have reversed the change.
 
glen viet
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie, copied!
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my variation :


 
glen viet
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Carey Brown, your idea of creating a temporary variable to hold keys solved my problem. Actually the missing values to check were in map1.
@salvin Francis, streams still complicated for me, hopefully, I will get acquainted with time.
BTW in terms of performance, please let me know if the solution is feasible because am dealing with biological markers that run into millions.
Thank you all for your thoughts and inputs.

Output: {k1=v11, k2=na, k4=v44}

Solution:

 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost, but not quite. The IF statement is critical because null will be returned if the key is not found and, of course, v2 could also be "na".
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, "in the millions"? Is that for both map1 and map2 or is one bigger than the other?

If one is smaller it would probably be more efficient to loop through that map and then do a lookup in the larger map.
 
glen viet
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Carey Brown, Noted about returning null -if no key in map2 or map2 having "na". For my scenarios that is fine, thanks. Map1 is smaller than map2, will follow your advice and again I really appreciate your time.
 
Carey Brown
Saloon Keeper
Posts: 9739
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're very welcome. Goodnight (at least it is where I am)
 
glen viet
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Carey Brown! Sure, good night ! You make programming exciting for biologists like us. A cool afternoon here in Japan. Bye.
 
I think she's lovely. It's this tiny ad that called her crazy:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic