我想存储一组对象,这些对象根据它们代表的值进行键控。这些键可以重复。例如:
[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.
发布评论
评论(4)
您需要一个Multimap。 Guava 提供了该接口以及各种子接口,例如
ListMultimap
、SetMultimap< /code> 和
SortedSetMultimap
取决于您希望将值存储在哪种集合中。然后它提供各种实现,例如ArrayListMultimap
和HashMultimap
,以及 中与它们一起使用的各种实用程序多重地图。在 Java 中执行此操作的传统方法类似于
Map>
、Map>
等,但是维护值集合是乏味的,并且各种本应简单的操作(例如仅为键添加值)却比需要的复杂得多。Multimap
旨在作为一种数据结构,专门设计用于对映射到单个键的多个值进行建模(与Map
不同)。鉴于此,它使操作像您期望的那样简单:如果您想确保相同的值不会两次映射到单个键并且不关心值的顺序,您可以使用
SetMultimap
而不是ListMultimap
等。我不确定你的意思是“是否存在另一个对象,其键与当前键的距离在 1 以内?如果是,则匹配他们把它们从收藏中删除。”但如果我没看错的话,您可以执行以下操作:
或者,您可以使用 TreeMultimap 执行某些操作,它将对其键集和值集进行排序。
You want a Multimap. Guava provides this interface and various subinterfaces such as
ListMultimap
,SetMultimap
andSortedSetMultimap
depending on what kind of collection you want values to be stored in. It then provides various implementations such asArrayListMultimap
andHashMultimap
, 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 (unlikeMap
). Given that, it makes operations as simple as you'd expect: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 aListMultimap
, 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:
Alternatively you could do something with a
TreeMultimap<Integer, String>
which will have both its key set and value sets sorted.以某种避免碰撞的方式使用
Map>
怎么样?它会将您的数据集更改为如下所示:您必须稍微更改迭代器代码。您可以使用
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: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 yourMap
with empty lists or make sure to check yourMap
values fornull
. It will sort of depend on how you generate yourMap
.怎么样:
我必须管理,我不 100% 遵循业务逻辑,但如果你可以为一个键拥有多个值,那么这样的东西应该支持它。
- 编辑 -
忽略这一点 - 有人向我指出这被标记为 java 问题。因此,回答为:
更为合适。
What about something like:
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:
is much more appropriate.
也许只是这样:列出 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.