我需要哪种 Java 对象类型(集合/列表/集合/其他)?

发布于 2024-10-06 06:09:56 字数 338 浏览 5 评论 0 原文

我想存储一组对象,这些对象根据它们代表的值进行键控。这些键可以重复。例如:

 [4] => Bob
 [5] => Mary
 [5] => Sue
 [9] => Steve
[10] => Jason
[10] => Michelle

本质上,我想循环遍历这个并查看每个键并说,“是否存在另一个对象(在本例中为人),其键与当前键的距离在 1 以内?如果是,则将它们匹配并将它们从收藏。”我将迭代上面示例中的“1”值,直到集合为空(或者在奇数场景中剩余一个对象)。

我不相信我尝试的方式是最好的方式,所以我也愿意接受反馈。

I want to store a collection of objects that are keyed based upon a value they represent. These keys can be repeated. e.g.:

 [4] => Bob
 [5] => Mary
 [5] => Sue
 [9] => Steve
[10] => Jason
[10] => Michelle

Essentially, I want to loop through this and look at each key and say, "is there another object (person in this case) whose key is within 1 from the current key? If so, match them up and remove them from the collection." I'm going to iterate the "1" value in the example above until the collection is empty (or has one object remaining for odd-numbered scenarios).

I'm not convinced the way I'm trying to go about this is the best way, so I'm open to feedback too.

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

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

发布评论

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

评论(4

凉世弥音 2024-10-13 06:09:56

您需要一个MultimapGuava 提供了该接口以及各种子接口,例如 ListMultimapSetMultimap< /code> 和 SortedSetMultimap 取决于您希望将值存储在哪种集合中。然后它提供各种实现,例如 ArrayListMultimapHashMultimap ,以及 中与它们一起使用的各种实用程序多重地图

在 Java 中执行此操作的传统方法类似于 Map>Map> 等,但是维护值集合是乏味的,并且各种本应简单的操作(例如仅为键添加值)却比需要的复杂得多。

Multimap 旨在作为一种数据结构,专门设计用于对映射到单个键的多个值进行建模(与 Map 不同)。鉴于此,它使操作像您期望的那样简单:

ListMultimap<Integer, String> m = ArrayListMultimap.create();
m.put(4, "Bob");
m.put(5, "Mary");
m.put(5, "Sue");
...

for (String name : m.get(5)) { ... } // iterates ["Mary", "Sue"]

如果您想确保相同的值不会两次映射到单个键并且不关心值的顺序,您可以使用 SetMultimap 而不是 ListMultimap 等。

我不确定你的意思是“是否存在另一个对象,其键与当前键的距离在 1 以内?如果是,则匹配他们把它们从收藏中删除。”但如果我没看错的话,您可以执行以下操作:

for (Integer key : m.keySet()) {
  Collection<String> people = m.get(key);
  Collection<String> peopleOneLower = m.get(key - 1); // empty if there are none
  ...
}

或者,您可以使用 TreeMultimap 执行某些操作,它将对其键集和值集进行排序。

You want a Multimap. Guava provides this interface and various subinterfaces such as ListMultimap, SetMultimap and SortedSetMultimap depending on what kind of collection you want values to be stored in. It then provides various implementations such as ArrayListMultimap and HashMultimap, plus various utlities for use with them in Multimaps.

The traditional way of doing this in Java is something like Map<K, List<V>>, Map<K, Set<V>> etc., but maintaining the values collections is tedious and various operations that should be simple (such as just putting a value for a key) are much more complicated than they need to be.

Multimap is intended as a data structure specifically designed to model multiple values mapped to a single key (unlike Map). Given that, it makes operations as simple as you'd expect:

ListMultimap<Integer, String> m = ArrayListMultimap.create();
m.put(4, "Bob");
m.put(5, "Mary");
m.put(5, "Sue");
...

for (String name : m.get(5)) { ... } // iterates ["Mary", "Sue"]

If you wanted to ensure the same value isn't mapped to a single key twice and don't care about the order the values are in, you can use a SetMultimap instead of a ListMultimap, etc.

I'm not sure what you mean as far as "is there another object whose key is within 1 from the current key? If so, match them up and remove them from the collection." But if I'm reading it right, you could do something like this:

for (Integer key : m.keySet()) {
  Collection<String> people = m.get(key);
  Collection<String> peopleOneLower = m.get(key - 1); // empty if there are none
  ...
}

Alternatively you could do something with a TreeMultimap<Integer, String> which will have both its key set and value sets sorted.

悲凉≈ 2024-10-13 06:09:56

以某种避免碰撞的方式使用 Map> 怎么样?它会将您的数据集更改为如下所示:

 [4] => [Bob]
 [5] => [Mary, Sue]
 [9] => [Steve]
[10] => [Jason, Michelle]

您必须稍微更改迭代器代码。您可以使用 List 元素的索引作为它们的“键”,当然,您必须添加逻辑以使用空列表初始化您的 Map 或确保检查您的 Map 值是否为 null。这在某种程度上取决于您生成地图的方式。

How about a Map<Integer, List<String>>, in a sort of collision-avoiding manner. It would change your data set to look like:

 [4] => [Bob]
 [5] => [Mary, Sue]
 [9] => [Steve]
[10] => [Jason, Michelle]

You would have to change your iterator code a bit. You would use the indexes of the List elements as their "keys", and of course you'd have to add logic to initialize your Map with empty lists or make sure to check your Map values for null. It will sort of depend on how you generate your Map.

遇见了你 2024-10-13 06:09:56

怎么样:

IDictionary<int,IList<Person>> 

我必须管理,我不 100% 遵循业务逻辑,但如果你可以为一个键拥有多个值,那么这样的东西应该支持它。

- 编辑 -
忽略这一点 - 有人向我指出这被标记为 java 问题。因此,回答为:

Map<Integer, List<String>>

更为合适。

What about something like:

IDictionary<int,IList<Person>> 

I have to admin, I don't 100% follow the business logic, but if you can have multiple values for a single key, something like this should support it.

-- Edit --
Ignore this - it was pointed out to me that this was tagged as a java question. Therefore, the response with:

Map<Integer, List<String>>

is much more appropriate.

ゃ懵逼小萝莉 2024-10-13 06:09:56

也许只是这样:列出 CustomObject 具有您的键和值的位置,然后按键对列表进行排序。

因为听起来您正在按顺序处理它们,所以我认为 Map 可能有点矫枉过正,因为您希望按顺序迭代键并成对删除项目。

Maybe just something like: List where the CustomObject has your key and value and you sort the list by the key.

Since it sounds like you are processing them in order, I think a Map might be overkill since you would want to iterate over the keys in order and be removing items in pairs.

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