How to name a HashMap in Java?

JavaNaming Conventions

Java Problem Overview

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?

Java Solutions

Solution 1 - Java

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

Solution 2 - Java

I like this question because Java does not allow map access via an operator like []. In other languages we could say things like






or (in Scala, this is cool)


and on and on. None of these work in Java, because of that silly get word!



EDIT: I actually think countyCounts is the best answer (IMHO); I was just making the point that the need for get limits one's choices.

Solution 3 - Java

Firstly avoid putting implementation details in the name of a variable, e.g. StateToCountyMap. Today you are using a Map interface but tomorrow you can decide to store your data in another type and the name of your variable has to be refactored otherwise it will become invalid and misleading.

Your data represents association of a state to its number of counties, so my advice is to name the variable as stateToNumberOfCounties. The key point in this name is to which designates that this variable stores an association, the part before that is the key and the part after is the value or values that are associated to the corresponding key. Also it would be indistinct to name the variable as stateToCountyNumber because one could not tell if it stores relation between a state and its number of counties or it stores a single number that represents the number of state to county associations, thus you would have to go back and forth in your code to check if it is of type Map or of type int.

Solution 4 - Java

I would call it numCounties or countyCounts.

Solution 5 - Java

I found a great case for countiesByState here:

> valuesByKeys, as in teamsByCaptains. If you're going to include both key and value, this seems to read best. At a high level, you can read it as just "teams", so anything that's performed on it is being performed on teams. The "byCaptains" prefix reads as it should do: a less significant qualifier that follows the teams around to help someone understand the structure if they need to.

This also allows you to access a value in a JSP with the nice notation countiesByState[myState].

Solution 6 - Java

Whatever explains it best - In this case stateToCountyMap is ok or else countiesInStateMap can be used.

Solution 7 - Java

I would call it mapStatesbyCountyCount, then again its bit lengthy variable name...

Solution 8 - Java

My opinion would be to have it countiesCountOfStateMap or simply countiesOfStateMap since we will be getting the counties count using the State.

It would be more meaningful to have it like this, so when new person looks into your code atleast he would be aware of what is contained in it.

But at the end of the day its your decision to have the appropriate name and as @tulskiy mentioned naming the class and variable appropriately is one of the harder things.

Solution 9 - Java

Storing states and counties in that variable it would be confusing to name it map - call it stateCountyHash or numCountiesByState


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser396089View Question on Stackoverflow
Solution 1 - JavaRAYView Answer on Stackoverflow
Solution 2 - JavaRay ToalView Answer on Stackoverflow
Solution 3 - JavaDimitar BonevView Answer on Stackoverflow
Solution 4 - JavaMatt BallView Answer on Stackoverflow
Solution 5 - JavaNoumenonView Answer on Stackoverflow
Solution 6 - JavaNrjView Answer on Stackoverflow
Solution 7 - JavaCharith De SilvaView Answer on Stackoverflow
Solution 8 - JavaraksjaView Answer on Stackoverflow
Solution 9 - JavaTudor ConstantinView Answer on Stackoverflow