Hi I have a LinkedHashMap (called info) that contains name/age (string/int) pairs. I want to find out, how can I get the position of the key/value if i input the key. For example, if my LinkedHashMap looked like this {bob=12, jeremy=42, carly=21} and I was to search jeremy, it should return 1 as its in position 1. I was hoping I can use something like info.getIndex("jeremy")
嗨,我有一个包含名称/年龄(字符串/ int)对的LinkedHashMap(称为info)。我想知道,如果我输入密钥,如何获得键/值的位置。例如,如果我的LinkedHashMap看起来像这样{bob = 12,jeremy = 42,carly = 21}并且我要搜索jeremy,它应该返回1作为它的位置1.我希望我可以使用类似info.getIndex的东西( “杰里米”)
4 个解决方案
#1
26
HashMap
implementations in general are un-ordered for Iteration
.
一般来说,HashMap实现对于迭代是无序的。
LinkedHashMap
is predictablely ordered for Iteration
( insertion order ) but does not expose the List
interface and a LinkedList
( which is what mirrors the key set insertion order ) does not track index position itself either, it is very in-efficient to find the index as well. The LinkedHashMap
doesn't expose the reference to the internal LinkedList
either.
LinkedHashMap可预测为迭代(插入顺序)排序,但不公开List接口,而LinkedList(这是镜像键插入顺序的镜像)也不跟踪索引位置本身,查找索引非常低效好。 LinkedHashMap也不公开对内部LinkedList的引用。
The actual "Linked List" behavior is implementation specific. Some may actually use an instance of
LinkedList
some many just haveEntry
track a previous and nextEntry
and use that as its implementation. Don't assume anything without looking at the source.实际的“链接列表”行为是特定于实现的。有些人实际上可能使用LinkedList的一个实例,有些人只有Entry跟踪上一个和下一个Entry并将其用作其实现。不看源头就不要假设任何事情。
The KeySet
that contains the keys does not guarantee order as well because of the hashing algorithms used for placement in the backing data structure of the inherited HashMap
. So you can't use that.
由于用于放置在继承的HashMap的后备数据结构中的散列算法,包含键的KeySet也不保证顺序。所以你不能使用它。
The only way to do this, without writing your own implementation, is to walk the Iterator
which uses the mirroring LinkedList
and keep a count where you are, this will be very in-efficient with large data sets.
在没有编写自己的实现的情况下,执行此操作的唯一方法是遍历使用镜像LinkedList的Iterator并保持计数,这对于大型数据集来说效率非常低。
Solution
What it sounds like you want is original insertion order index positions, you would have to mirror the keys in the KeySet
in something like an ArrayList
, keep it in sync with updates to the HashMap
and use it for finding position. Creating a sub-class of HashMap
, say IndexedHashMap
and adding this ArrayList
internally and adding a .getKeyIndex(<K> key)
that delegates to the internal ArrayList
.indexOf()
is probably the best way to go about this.
你想要的是原始的插入顺序索引位置,你必须镜像KeyList中的键,类似于ArrayList,使其与HashMap的更新保持同步,并用它来寻找位置。创建一个HashMap的子类,比如IndexedHashMap并在内部添加这个ArrayList并添加一个委托给内部ArrayList .indexOf()的.getKeyIndex(
This is what LinkedHashMap
does but with a LinkedList
mirroring the KeySet
instead of an ArrayList
.
这是LinkedHashMap所做的事情,但是使用LinkedList镜像KeySet而不是ArrayList。
#2
10
int pos = new ArrayList<String>(info.keySet()).indexOf("jeremy")
#3
0
LinkedHashMap has "predictable iteration order" (javadoc). Items don't know their location, though, so you'll have to iterate the collection to get it. If you're maintaining a large map you may want to use a different structure for storage.
LinkedHashMap具有“可预测的迭代顺序”(javadoc)。但是,项目不知道它们的位置,因此您必须迭代该集合才能获得它。如果您要维护大型地图,则可能需要使用其他结构进行存储。
Edit: clarified iteration
编辑:澄清迭代
#4
0
You can use com.google.common.collect.LinkedListMultimap
from the Google Guava library. You don't need the multimap behaviour of this class what you want is that the keys()
method guarantees they are returned in insertion order and can then be used to construct a List, you can use the indexOf()
to find the required index position
您可以使用Google Guava库中的com.google.common.collect.LinkedListMultimap。你不需要这个类的multimap行为你想要的是keys()方法保证它们以插入顺序返回,然后可以用来构造List,你可以使用indexOf()来查找所需的索引位置
#1
26
HashMap
implementations in general are un-ordered for Iteration
.
一般来说,HashMap实现对于迭代是无序的。
LinkedHashMap
is predictablely ordered for Iteration
( insertion order ) but does not expose the List
interface and a LinkedList
( which is what mirrors the key set insertion order ) does not track index position itself either, it is very in-efficient to find the index as well. The LinkedHashMap
doesn't expose the reference to the internal LinkedList
either.
LinkedHashMap可预测为迭代(插入顺序)排序,但不公开List接口,而LinkedList(这是镜像键插入顺序的镜像)也不跟踪索引位置本身,查找索引非常低效好。 LinkedHashMap也不公开对内部LinkedList的引用。
The actual "Linked List" behavior is implementation specific. Some may actually use an instance of
LinkedList
some many just haveEntry
track a previous and nextEntry
and use that as its implementation. Don't assume anything without looking at the source.实际的“链接列表”行为是特定于实现的。有些人实际上可能使用LinkedList的一个实例,有些人只有Entry跟踪上一个和下一个Entry并将其用作其实现。不看源头就不要假设任何事情。
The KeySet
that contains the keys does not guarantee order as well because of the hashing algorithms used for placement in the backing data structure of the inherited HashMap
. So you can't use that.
由于用于放置在继承的HashMap的后备数据结构中的散列算法,包含键的KeySet也不保证顺序。所以你不能使用它。
The only way to do this, without writing your own implementation, is to walk the Iterator
which uses the mirroring LinkedList
and keep a count where you are, this will be very in-efficient with large data sets.
在没有编写自己的实现的情况下,执行此操作的唯一方法是遍历使用镜像LinkedList的Iterator并保持计数,这对于大型数据集来说效率非常低。
Solution
What it sounds like you want is original insertion order index positions, you would have to mirror the keys in the KeySet
in something like an ArrayList
, keep it in sync with updates to the HashMap
and use it for finding position. Creating a sub-class of HashMap
, say IndexedHashMap
and adding this ArrayList
internally and adding a .getKeyIndex(<K> key)
that delegates to the internal ArrayList
.indexOf()
is probably the best way to go about this.
你想要的是原始的插入顺序索引位置,你必须镜像KeyList中的键,类似于ArrayList,使其与HashMap的更新保持同步,并用它来寻找位置。创建一个HashMap的子类,比如IndexedHashMap并在内部添加这个ArrayList并添加一个委托给内部ArrayList .indexOf()的.getKeyIndex(
This is what LinkedHashMap
does but with a LinkedList
mirroring the KeySet
instead of an ArrayList
.
这是LinkedHashMap所做的事情,但是使用LinkedList镜像KeySet而不是ArrayList。
#2
10
int pos = new ArrayList<String>(info.keySet()).indexOf("jeremy")
#3
0
LinkedHashMap has "predictable iteration order" (javadoc). Items don't know their location, though, so you'll have to iterate the collection to get it. If you're maintaining a large map you may want to use a different structure for storage.
LinkedHashMap具有“可预测的迭代顺序”(javadoc)。但是,项目不知道它们的位置,因此您必须迭代该集合才能获得它。如果您要维护大型地图,则可能需要使用其他结构进行存储。
Edit: clarified iteration
编辑:澄清迭代
#4
0
You can use com.google.common.collect.LinkedListMultimap
from the Google Guava library. You don't need the multimap behaviour of this class what you want is that the keys()
method guarantees they are returned in insertion order and can then be used to construct a List, you can use the indexOf()
to find the required index position
您可以使用Google Guava库中的com.google.common.collect.LinkedListMultimap。你不需要这个类的multimap行为你想要的是keys()方法保证它们以插入顺序返回,然后可以用来构造List,你可以使用indexOf()来查找所需的索引位置