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.
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.
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.
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.