Java 中有“标记”集合吗?为了澄清,我想本质上使用某种对象(例如字符串)对列表中的每个元素进行分类,以便我可以通过我指定的标签而不是通过对象类型或元素内容引用列表中的不同对象。
编辑:HashMap 将不起作用,因为列表中项目的顺序也被认为很重要。我的理解是,当列表的顺序在某种程度上很重要时,HashMap 并不实用。如果我在这件事上有不正确的地方,请务必纠正我。
Are there "tagged" collections in Java? To clarify, I want to essentially classify each element in the list with some sort of Object (e.g. a String) so I can reference different objects in the list by a tag that I specify, not by Object type or element contents.
EDIT: A HashMap will not work as the order of the items in the list is also deemed to be important. My understanding is that a HashMap is not practical when the order of the list matters in some way. If I am incorrect on this matter, by all means please correct me.
发布评论
评论(10)
您正在寻找
地图
。此数据结构将键映射到值。You're looking for a
Map
. This data structure maps keys to values.如果 Map 中的每个键映射到一组对象,您将能够轻松查找具有特定标签(键)的每个对象
if each key in a Map maps to a Set of objects, you'll be able to easily look up every object with a particular tag(key)
不,据我所知,标准 Java 库中没有“标记”集合。但是您可以使用
Map>
来标记字符串。No, as far as I know there are no "tagged" collections in the standard Java libraries. But you could use a
Map<Tag,Set<String>>
to tag strings.您可以使用地图
您可以将字符串替换为您想要的任何内容。您可能会遇到的一个问题是,如果您想使用相同的“标签”对多个对象进行分类。例如,“Cat”标签可能适用于多个唯一的猫对象,而 hashMap 只能让“Cat”标签指向一个 Cat 对象。如果这就是您想要做的(一个标签指向多个对象),您需要将映射声明为:
这将创建一个 HashMap,其中包含每个对象的列表,以便您可以拥有一个标签指向的多个对象。
You could use a map
You can replace String with whatever you want. The one problem you might run into though, is if you want to categorize multiple objects with the same "tag". For instance, a "Cat" tag might apply to multiple, unique cat objects and a hashMap would only let you have one Cat object pointed to by the "Cat" tag. If that's what you want to do (multiple objects pointed to by one tag), you will need to declare the map as:
That will create a HashMap holding a list for each object so you can have multiple objects pointed to by one tag.
如果您不受 JDK 中的内容的限制,请考虑使用 番石榴多重地图。它们为集合图提供预先构建的支持。
If you're not restricted to what's in the JDK, consider use of Guava Multimaps. They provide pre-built support for a map of sets.
如果顺序很重要,则使用 LinkedHashMap。
If order is important then use a LinkedHashMap.
也许
Map
正是您所需要的Perhaps a
Map
is what you need这个还不够详细。我们不知道是否:
- 每个项目允许有多个标签
- 每个标签允许有多个项目
我将寻求最广泛的解决方案,这两者都是正确的。
您首先需要的是您的 Tag 类(我建议使用枚举),
然后您的集合将是一个 Map,其中键是您的对象,值是每个对象的标签列表。
当然,如果您想允许任何标签而不仅仅是已知标签,则可以将“Tag”替换为字符串。
根据你想对你的集合做什么,你可能想要逆转它:
我建议首先编写列出你的规范的测试用例(单元测试),然后你的集合的设计将从它们中有机地出现(TDD)。
This is not detailed enough. We don't know if:
- multiple tags per item are allowed
- multiple items per tag are allowed
I'm going to go for the widest solution, where both of these are true.
First thing you need is your Tag class (I suggest an enum)
Then your collection will be a Map where the key is your object, and the value your list of tags for each object.
Of course, you can replace "Tag" with a String, if you want to allow any tag and not just known tags.
And depending what you want to do with your collection, you may want to reverse it:
I suggest first writing your test cases (unit-tests) listing your specifications, and the design of your collection will appear organically from them (TDD).
请参阅 SortedMap 和 TreeMap。
See SortedMap and TreeMap.
听起来你想要一个 hashmap
sounds like you want a hashmap