本文是小编为大家收集整理的关于如何从Java映射中获取元素位置的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我有此Java地图:
您能告诉我如何获得地图的第6个元素?
private static final Map<String, Users> cache = new HashMap<>();
这可能吗?还是我必须使用另一个Java集合?
推荐答案
虽然回答有点晚.但是选项是使用 LinkedHashMap 正如每个人所建议的那样,根据元素的插入订单.但是,作为警告,它具有构造函数LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder),该构造函数将创建一个链接的哈希地图,其迭代顺序是其条目是最后一个accessed的顺序.不要在这种情况下使用此构造函数.
但是,如果我需要这样的功能,我将扩展它并实施我的必要功能以重复使用它们.
class MyLinkedMap<K, V> extends LinkedHashMap<K, V> { public V getValue(int i) { Map.Entry<K, V>entry = this.getEntry(i); if(entry == null) return null; return entry.getValue(); } public Map.Entry<K, V> getEntry(int i) { // check if negetive index provided Set<Map.Entry<K,V>>entries = entrySet(); int j = 0; for(Map.Entry<K, V>entry : entries) if(j++ == i)return entry; return null; } }
现在我可以实例化,并可以获得我想要的任何一种输入和价值:
MyLinkedMap<String, Integer>map = new MyLinkedMap<>(); map.put("a first", 1); map.put("a second", 2); map.put("a third", 3); System.out.println(map.getValue(2)); System.out.println(map.getEntry(1));
输出:
3 a second=2
其他推荐答案
HashMap未授予订单.如果您担心订单,则应使用LinkedHashMap
Map<String, Users> orderedMap=new LinkedHashMap<>();
现在,当您放置一个元素时,它将保留您的订单.
如果您想获得第六个元素,现在可以做到这一点,因为您的元素按顺序有序.
orderedMap.values().toArray()[5]// will give you 6th value in the map.
示例
Map<String, String> orderedMap=new LinkedHashMap<>(); orderedMap.put("a","a"); orderedMap.put("b","b"); System.out.println(orderedMap.values().toArray()[1]); // you will get b(value) System.out.println(orderedMap.keySet().toArray()[1]); // you will get b(key) }
其他推荐答案
a HashMap无法维护其中插入的元素的顺序.您可以使用LinkedHashMap,而不是维护其中插入的元素的顺序.
尽管您需要注意,即使是LinkedHashMap也没有这样的方法,该方法可以给出特定索引的元素.您将必须通过条目手动迭代并在第六次迭代中提取元素.
问题描述
I have this Java Map:
Can you tell me how I can get the 6-th element of the Map?
private static final Map<String, Users> cache = new HashMap<>();
is this possible? Or I have to use another Java collection?
推荐答案
Though a bit late to answer. But the option is to use LinkedHashMap: this map preserves the order according to insertion of elements, as everyone has suggested. However, As a warning, it has a constructor LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) which will create a linked hash map whose order of iteration is the order in which its entries were last accessed. Don't use this constructor for this case.
However, if I needed such functionality, i would extend it and implement my necessary function to re-use them in OOP way.
class MyLinkedMap<K, V> extends LinkedHashMap<K, V> { public V getValue(int i) { Map.Entry<K, V>entry = this.getEntry(i); if(entry == null) return null; return entry.getValue(); } public Map.Entry<K, V> getEntry(int i) { // check if negetive index provided Set<Map.Entry<K,V>>entries = entrySet(); int j = 0; for(Map.Entry<K, V>entry : entries) if(j++ == i)return entry; return null; } }
Now i can instantiate it and can get a entry and value either way i want:
MyLinkedMap<String, Integer>map = new MyLinkedMap<>(); map.put("a first", 1); map.put("a second", 2); map.put("a third", 3); System.out.println(map.getValue(2)); System.out.println(map.getEntry(1));
Output:
3 a second=2
其他推荐答案
HashMap doesn't grantee the order. If you concern about order you should use LinkedHashMap
Map<String, Users> orderedMap=new LinkedHashMap<>();
Now when you put an element it will keep the order what you put.
If you want to get 6th element, now you can do it since you have your elements in order.
orderedMap.values().toArray()[5]// will give you 6th value in the map.
Example
Map<String, String> orderedMap=new LinkedHashMap<>(); orderedMap.put("a","a"); orderedMap.put("b","b"); System.out.println(orderedMap.values().toArray()[1]); // you will get b(value) System.out.println(orderedMap.keySet().toArray()[1]); // you will get b(key) }
其他推荐答案
A HashMap does not maintain the order of the elements inserted in it. You can used a LinkedHashMap instead which maintains the order of the elements inserted in it.
Though you need to note that even a LinkedHashMap has no such method which would give the element at a particular index. You will have to manually iterate through the entries and extract the element at the 6th iteration.