Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Getting custom key from map based on comparator etc.

 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the code scenario as below. The map contains both Key and Value as beans. I want to get value from map, even when all fields of the key may not be present i.e. are null. Is there a way to do this?



So precisely, i want the K3 to work as well somehow. Not sure if I am thinking straight , but need this or something on this line to work
Thanks in advance.

- Anubhav
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anubhav Anand wrote:i want the K3 to work as well somehow.


what value you are expecting? what are the attribute should be unique in the Key object?
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets say for this example, age will always be unique. But I don't want to maintain the same set of vales for age and name keys into multiple maps.
So, in this case I want K3 to return the value same as K2.

Thanks,
Anubhav
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then change your equals and hashCode implementation to

i.e, exclude the name attribute from your equals and hasCode implementation
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seetharaman.
Is there a way to also achieve if-else. Precisely what if say the name was unique as well independently. Because the actual key bean has a lot of attributes say 10, and the combination of all 10 would be unique, but at times even 6 may be able to provide the required unique constraint.
So, i want to achieve something like the nvl function in SQL.

So, in this case make it work for say name, or age, or both.

Thanks,
Anubhav
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sort of, implement your equals and hashCode as your needs.
 
Marshal
Posts: 75697
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:then change your equals and hashCode implementation to
. . .

That equals method won't work; it will be prone to Exceptions because of the cast. You need to test for the type of the obj parameter before using it. I would suggest you write it like this.This will still suffer problems if you allow null values for age and name. Then you would be better off like this:
... ((Key)obj).age == null ? this.age == null : ((Key)obj).age.equals(this.age) && ...
Obviously you can use as many fields or as few as you wish in the equals method, but yo umust use the same fields in the hashCode method.
 
Nothing up my sleeve ... and ... presto! A tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic