在我的java编码中,我经常会得到几个 Map>
或 Map>
然后我很难记住哪个字符串是哪个键。我用 //Map>
或 //Map
注释该声明,但这不是最好的解决方案。如果 String 不是最终的,我会创建新类 CapabilityId extends String
和 GroupId extends String
,但我不能。有没有更好的方法来跟踪哪个东西是关键并且也许让编译器强制执行它?
In my java coding, I often end up with several Map<String,Map<String,foo>>
or Map<String,List<String>>
and then I have trouble remembering which String is which key. I comment the declaration with //Map<capabiltyId,Map<groupId,foo>>
or //Map<groupId,List<capabilityId>
, but it's not the greatest solution. If String wasn't final, I would make new classes CapabilityId extends String
and GroupId extends String
, but I can't. Is there a better way to keep track of which thing is the key and maybe have the compiler enforce it?
发布评论
评论(7)
我会将其全部放在一个类中,并使用合理的字段/方法/参数名称。
这样您就可以在方法/参数名称中看到它期望/返回的内容。
I would put it all in single class and make use of sensible field/method/argument names.
This way the you can see at method/argument names what it expects/returns.
有很多方法可以实现这一点(有些已经提到了):
Map>
看起来非常像您有一个复合键,即包含两部分的键。因此,引入一个不可变的复合键类,即代表两个组件值对象的值对象。There are a number of ways to go on this one (some already mentioned):
Map<String,Map<String,foo>>
looks very much like you have a composite key, i.e. a key that comprises two parts. So, introduce an immutable composite key class, that is a value object representing the two component value objects.Map>
您应该使用 Google Guava / Google Collection http://google-collections.googlecode.com/svn/trunk/javadoc/index.html?com/google /common/collect/Multimap.html
Instead of
Map<String,List<String>>
you should use Multimap from Google Guava / Google Collectionhttp://google-collections.googlecode.com/svn/trunk/javadoc/index.html?com/google/common/collect/Multimap.html
添加到其他答案:
包装它。
这不仅是解决您的问题的方法,而且总体上是一个好主意,即避免简单的
参数。您的代码将获得可读性、健全性和可维护性。
您可以向其添加各种不错的属性,例如声明它@Immutable。正如您发现的那样,这种方式更容易记住和控制。您拥有班级并可以用它做任何您喜欢的事情。
Adding to the other answers:
Wrap it.
It is not just a solution to your problem but a good idea in general, i.e. avoid simple
parameters. Your code will gain readability, sanity and maintainability.
You can add all kinds of nice properties to it, e.g. declare it @Immutable. As you found out it this way is better to remember and to control. You own the class and can do whatever you like with it.
如果需要,可以将字符串包装在包装类中:
Wrap strings in wrapper-classes if you want:
CapabilityId
可以包含一个名为“id”的String
字段,而不是让CapabilityId
扩展String
;那么您的Map
可以定义为Map>
,并且您可以通过getId( )
在你的关键类上。我不确定我自己会这样做,但如果我这样做,我可能会这样做。
您可以通过使用带有 id 字段和
getId()
方法的abstract GenericId
类来限制混乱,并具有CapabilityId
和GroupId
继承自它。Instead of having
CapabilityId
extendString
,CapabilityId
could include aString
field called "id"; then yourMap
could be defined asMap<CapabilityId, Map<GroupId, Foo>>
, and you could get at the individual ID fields through agetId()
on your key classes.I'm not sure I would do this myself, but if I did, this is probably what I'd do.
You could limit the clutter by having an
abstract GenericId
class with an id field andgetId()
method, and haveCapabilityId
andGroupId
inherit from it.创建一个可以子类化的
ID
类,该类由一个String
字段以及equals()
和hashCode() 的实现组成
使用该字段。Create an
ID
class which you can subclass, and which consists of aString
field and implementations ofequals()
andhashCode()
which use that field.