This question already has an answer here:
这个问题在这里已有答案:
- How to get position of key/value in LinkedHashMap using its key 4 answers
- 如何使用其关键4答案获取LinkedHashMap中键/值的位置
I have
我有
LinkedHashMap<String, List<String>> hMap;
I want to get List<String>
by position not on key.
我想按位置而不是键来获取List
I don't want to use iterate.
我不想使用迭代。
Is there any other way to get Value based on index ?
有没有其他方法可以根据索引获得价值?
5 个解决方案
#1
44
You can't get the value of the Map
based on index, Map
s just don't work that way. A workaround would be to create a new list from your values and get the value based on index.
您无法根据索引获取Map的值,Maps只是不能以这种方式工作。解决方法是从您的值创建新列表并根据索引获取值。
LinkedHashMap<String, List<String>> hMap;
List<List<String>> l = new ArrayList<List<String>>(hMap.values());
l.get(0);
#2
14
public List<String> getByIndex(LinkedHashMap<String, List<String>> hMap, int index){
return (List<String>) hMap.values().toArray()[index];
}
#3
10
you may want to consider either using another class to store your data, or write an extension to the linkedHashMap. something like
您可能想要考虑使用另一个类来存储数据,或者编写一个扩展到linkedHashMap。就像是
//this is pseudo code
public class IndexedLinkedHashMap<K,V> extends LinkedHashMap{
HashMap<int,K> index;
int curr = 0;
@Override
public void add(K key,V val){
super.add(key,val);
index.add(curr++, key);
}
public V getindexed(int i){
return super.get(index.get(i));
}
}
#4
5
As Kevin Bowersox stated, it's as simple as
正如Kevin Bowersox所说,它就像
List<String> result = (List<String>) hMap.values().toArray()[position];
But it should be noted that this will still iterate by using .toArray(). It's a simple statement and I'm not sure if there is one with better performance, but be aware that complexity is not log(n) (like indexed access in case of B*), but just n. Since LinkedHashMap is based on LinkedList, there is no way to randomly access elements, only in sequential order.
但应该注意的是,这仍将通过使用.toArray()进行迭代。这是一个简单的陈述,我不确定是否有更好的性能,但要注意复杂性不是log(n)(如B *情况下的索引访问),但只是n。由于LinkedHashMap基于LinkedList,因此无法按顺序随机访问元素。
The cast to List is an unavoidable evil, since .toArray() follows the archaic concept of returning Object instead of a generic data type.
对List的强制转换是一个不可避免的恶魔,因为.toArray()遵循返回Object而不是通用数据类型的古老概念。
While this might not be the main concept of a map, LinkedHashMap isn't just a map. it extends HashMap, and as an extending class, it's perfectly fine to bring additional methods that support the idiosyncracies of that class.
虽然这可能不是地图的主要概念,但LinkedHashMap不仅仅是一张地图。它扩展了HashMap,作为一个扩展类,它可以带来支持该类特性的其他方法。
#5
3
There is no direct DS in the standard Java Collections API to provide a indexed map. However, the following should let you achieve the result:
标准Java Collections API中没有直接DS来提供索引映射。但是,以下应该可以让您实现结果:
// An ordered map
Map<K, V> map = new LinkedHashMap<K, V>();
// To create indexed list, copy the references into an ArrayList (backed by an array)
List<Entry<K, V>> indexedList = new ArrayList<Map.Entry<K, V>>(map.entrySet());
// Get the i'th term
<Map.Entry<K,V>> entry = indexedList.get(index);
K key = entry.getKey();
V value = entry.getValue();
You might still want to retain the concerns of data persistence in the map separate from the retrieval.
您可能仍希望保留地图中与检索分开的数据持久性问题。
Update: Or use LinkedMap from Apache Commons.
更新:或使用Apache Commons中的LinkedMap。
#1
44
You can't get the value of the Map
based on index, Map
s just don't work that way. A workaround would be to create a new list from your values and get the value based on index.
您无法根据索引获取Map的值,Maps只是不能以这种方式工作。解决方法是从您的值创建新列表并根据索引获取值。
LinkedHashMap<String, List<String>> hMap;
List<List<String>> l = new ArrayList<List<String>>(hMap.values());
l.get(0);
#2
14
public List<String> getByIndex(LinkedHashMap<String, List<String>> hMap, int index){
return (List<String>) hMap.values().toArray()[index];
}
#3
10
you may want to consider either using another class to store your data, or write an extension to the linkedHashMap. something like
您可能想要考虑使用另一个类来存储数据,或者编写一个扩展到linkedHashMap。就像是
//this is pseudo code
public class IndexedLinkedHashMap<K,V> extends LinkedHashMap{
HashMap<int,K> index;
int curr = 0;
@Override
public void add(K key,V val){
super.add(key,val);
index.add(curr++, key);
}
public V getindexed(int i){
return super.get(index.get(i));
}
}
#4
5
As Kevin Bowersox stated, it's as simple as
正如Kevin Bowersox所说,它就像
List<String> result = (List<String>) hMap.values().toArray()[position];
But it should be noted that this will still iterate by using .toArray(). It's a simple statement and I'm not sure if there is one with better performance, but be aware that complexity is not log(n) (like indexed access in case of B*), but just n. Since LinkedHashMap is based on LinkedList, there is no way to randomly access elements, only in sequential order.
但应该注意的是,这仍将通过使用.toArray()进行迭代。这是一个简单的陈述,我不确定是否有更好的性能,但要注意复杂性不是log(n)(如B *情况下的索引访问),但只是n。由于LinkedHashMap基于LinkedList,因此无法按顺序随机访问元素。
The cast to List is an unavoidable evil, since .toArray() follows the archaic concept of returning Object instead of a generic data type.
对List的强制转换是一个不可避免的恶魔,因为.toArray()遵循返回Object而不是通用数据类型的古老概念。
While this might not be the main concept of a map, LinkedHashMap isn't just a map. it extends HashMap, and as an extending class, it's perfectly fine to bring additional methods that support the idiosyncracies of that class.
虽然这可能不是地图的主要概念,但LinkedHashMap不仅仅是一张地图。它扩展了HashMap,作为一个扩展类,它可以带来支持该类特性的其他方法。
#5
3
There is no direct DS in the standard Java Collections API to provide a indexed map. However, the following should let you achieve the result:
标准Java Collections API中没有直接DS来提供索引映射。但是,以下应该可以让您实现结果:
// An ordered map
Map<K, V> map = new LinkedHashMap<K, V>();
// To create indexed list, copy the references into an ArrayList (backed by an array)
List<Entry<K, V>> indexedList = new ArrayList<Map.Entry<K, V>>(map.entrySet());
// Get the i'th term
<Map.Entry<K,V>> entry = indexedList.get(index);
K key = entry.getKey();
V value = entry.getValue();
You might still want to retain the concerns of data persistence in the map separate from the retrieval.
您可能仍希望保留地图中与检索分开的数据持久性问题。
Update: Or use LinkedMap from Apache Commons.
更新:或使用Apache Commons中的LinkedMap。