如何用Java命名HashMap? [英] How to name a HashMap in Java?
问题描述
这可能是一个愚蠢的问题,但是我从未找到一种令人满意的方式来在Java中命名类型为HashMap<K,V>
的变量.例如-假设我有一个HashMap
,其中每个存储桶都是<K,V>
对,其中K
是String
表示州",而V
是Integer
表示州县有.
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.
HashMap
是否应命名为"mapStateCounty
","stateToCountyMap
"等?
哪一个逻辑上听起来更吸引人并且更直观,却又不引起混淆和冗长?
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不允许通过像[]
这样的运算符进行地图访问.在其他语言中,我们可以说类似
I like this question because Java does not allow map access via an operator like []
. In other languages we could say things like
numberOfCountiesIn["HI"]
或
countyCountOf["CA"]
或
numCountiesIn->{"MA"}
或(在Scala中,这很酷)
or (in Scala, this is cool)
numCountiesIn("WA")
等等.这些都不在Java中起作用,因为这个get
愚蠢的单词!
and on and on. None of these work in Java, because of that silly get
word!
countyCounts.get("NY")
确实!
我实际上认为countyCounts
是最佳答案(IMHO);我只是指出,对get
的需求会限制一个人的选择.
I actually think countyCounts
is the best answer (IMHO); I was just making the point that the need for get
limits one's choices.
这篇关于如何用Java命名HashMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!