• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting custom key from map based on comparator etc.  RSS feed

 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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 Java Spring
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sort of, implement your equals and hashCode as your needs.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!