This might be a silly question, but I have never found a satisfying way to name a variable of type HashMap<K,V>
in Java. For example - lets say I have a HashMap
where every bucket is a <K,V>
pair where K
is a String
say representing "State" and V
is an Integer
representing the number of counties the state has.
Should the HashMap
be named as "mapStateCounty
", "stateToCountyMap
", etc. ?
Which one seems logically more appealing and intuitive to understand without sounding confusing and verbose?
Whatever explains it best - In this case
stateToCountyMap
is ok or elsecountiesInStateMap
can be used.Storing states and counties in that variable it would be confusing to name it map - call it
stateCountyHash
ornumCountiesByState
I would call it
numCounties
orcountyCounts
.I like this question because Java does not allow map access via an operator like
[]
. In other languages we could say things likeor
or
or (in Scala, this is cool)
and on and on. None of these work in Java, because of that silly
get
word!Indeed!
EDIT: I actually think
countyCounts
is the best answer (IMHO); I was just making the point that the need forget
limits one's choices.I don't believe there is a hard-written rule anywhere that tells you how to name your map, so as long as you come up with a rule that makes sense to you (and your teammates), then it should be fine.
Personally, I like to call my maps
keyToValue
orvalueByKey
.I found a great case for
countiesByState
here:This also allows you to access a value in a JSP with the nice notation
countiesByState[myState]
.