[英]Get LinkedHashMap index of a key
如何在LinkedHashMap中獲取鍵的索引?
例如我得到了這個:
index key value
0 "a" 10
1 "b" 20
2 "c" 30
現在,我想獲取鍵“ b”的索引。 在這種情況下是什么1。
有沒有O(n)運行時的方法嗎? 我將在循環中使用它,例如new ArrayList<String>(mapIndex.keySet()).indexOf(position_with_section_map[position]);
對我來說不是解決方案
不,沒有LinkedHashMap
的O(n)運行時就無法做到這一點。 您可以保留一個額外的Map<String, Integer>
來自己保留元素的索引,但這實際上是您唯一可以做的事情。
添加到Louis Wasserman答案而不是創建新地圖,您可以將索引和值移動到Pojo,然后將其另存為Map中的值。
class Test{
int index;
int value;
public int getIndex();
public int getValue();
}
使用LinkedHashMap作為LinkedHashMap字符串,測試>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.