java - Create a unique key for Map from multiple Enum types -
i have requirement multiple enums need find value in i.e. map<key,value>
(combination of enums return unique value). think there many options having wrapper object key act key. also, can use guava table if keys limited 2 (not sure).
wanted check below approach 2 enums map unique computed value, need suggestions understanding -
i) if approach fine?
ii) if yes, scalable? i.e. can made generic support 'n' enums in tokey(enum ...enums)
below snippet 2 enums -
static integer tokey(status s, substatus ss) { return integer.valueof(s.ordinal() * substatus.values().length + ss.ordinal()); }
and
status { none, pass, warn, reject } substatus { none, soft_reject, hard_reject } integer key1 = tokey(status.reject, substatus.hard_reject) integer key2 = tokey(status.warn, substatus.none) key1 != key2
thanks!
you can try code generate hash code serves key:
static int tokey(status s, substatus ss) { final int prime = 31; int result = 1; result = prime * result + ((s == null) ? 0 : s.hashcode()); result = prime * result + ((ss == null) ? 0 : ss.hashcode()); return result; }
it should robust against collisions due prime number , extendable support n enums.
Comments
Post a Comment