是否有“标记”? Java 中的集合?

发布于 2024-12-07 09:03:26 字数 218 浏览 0 评论 0 原文

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

过期以后 2024-12-14 09:03:26

您正在寻找地图。此数据结构将映射到

You're looking for a Map. This data structure maps keys to values.

青瓷清茶倾城歌 2024-12-14 09:03:26

如果 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)

一袭水袖舞倾城 2024-12-14 09:03:26

不,据我所知,标准 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.

凉栀 2024-12-14 09:03:26

您可以使用地图

Map<String, Object> = new HashMap<String, Object>(); 

您可以将字符串替换为您想要的任何内容。您可能会遇到的一个问题是,如果您想使用相同的“标签”对多个对象进行分类。例如,“Cat”标签可能适用于多个唯一的猫对象,而 hashMap 只能让“Cat”标签指向一个 Cat 对象。如果这就是您想要做的(一个标签指向多个对象),您需要将映射声明为:

Map<String, List<Object>> = new HashMap<String, List<Object>>(); 

这将创建一个 HashMap,其中包含每个对象的列表,以便您可以拥有一个标签指向的多个对象。

You could use a map

Map<String, Object> = new HashMap<String, Object>(); 

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:

Map<String, List<Object>> = new HashMap<String, List<Object>>(); 

That will create a HashMap holding a list for each object so you can have multiple objects pointed to by one tag.

追星践月 2024-12-14 09:03:26

如果您不受 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.

风流物 2024-12-14 09:03:26

如果顺序很重要,则使用 LinkedHashMap

If order is important then use a LinkedHashMap.

枕花眠 2024-12-14 09:03:26

也许 Map 正是您所需要的

Perhaps a Map is what you need

骑趴 2024-12-14 09:03:26

这个还不够详细。我们不知道是否:
- 每个项目允许有多个标签
- 每个标签允许有多个项目

我将寻求最广泛的解决方案,这两者都是正确的。
您首先需要的是您的 Tag 类(我建议使用枚举),

public enum Tag {
    TAG1, TAG2, TAG3; // add your tags here
}

然后您的集合将是一个 Map,其中键是您的对象,值是每个对象的标签列表。

Map<Object, List<Tag>> items = new HashMap<Object, List<Tag>>();

当然,如果您想允许任何标签而不仅仅是已知标签,则可以将“Tag”替换为字符串。

根据你想对你的集合做什么,你可能想要逆转它:

Map<Tag, List<Object>> = new HashMap<Tag, List<Object>>();

我建议首先编写列出你的规范的测试用例(单元测试),然后你的集合的设计将从它们中有机地出现(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)

public enum Tag {
    TAG1, TAG2, TAG3; // add your tags here
}

Then your collection will be a Map where the key is your object, and the value your list of tags for each object.

Map<Object, List<Tag>> items = new HashMap<Object, List<Tag>>();

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:

Map<Tag, List<Object>> = new HashMap<Tag, List<Object>>();

I suggest first writing your test cases (unit-tests) listing your specifications, and the design of your collection will appear organically from them (TDD).

指尖上得阳光 2024-12-14 09:03:26

请参阅 SortedMap 和 TreeMap

See SortedMap and TreeMap.

茶花眉 2024-12-14 09:03:26

听起来你想要一个 hashmap

sounds like you want a hashmap

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文