Java 获取键的LinkedHashMap索引
如何在LinkedHashMap中获取键的索引 例如,我得到了这个:Java 获取键的LinkedHashMap索引,java,hashmap,linkedhashmap,Java,Hashmap,Linkedhashmap,如何在LinkedHashMap中获取键的索引 例如,我得到了这个: index key value 0 "a" 10 1 "b" 20 2 "c" 30 现在我想得到键“b”的索引。在这种情况下是什么 没有O(n)运行时的任何方法? 我将在循环中使用它,例如newarraylist(mapIndex.keySet()).
index key value
0 "a" 10
1 "b" 20
2 "c" 30
现在我想得到键“b”的索引。在这种情况下是什么
没有O(n)运行时的任何方法?
我将在循环中使用它,例如newarraylist(mapIndex.keySet()).indexOf(position_with_section_map[position])代码>
对我来说,这不是一个解决方案。不,如果没有运行时的O(n),就无法从LinkedHashMap
实现这一点。您可以保留一个额外的映射
来保存元素的索引,但这确实是您唯一可以做的事情。添加到Louis Wasserman answer而不是创建新映射,您可以将索引和值移动到Pojo,并将其保存为映射中的值
class Test{
int index;
int value;
public int getIndex();
public int getValue();
}
将LinkedHashMap用作LinkedHashMap字符串,测试>如何在循环中使用它?根本没有使用它的API,更不用说在循环中了。您显示的索引值只有在条目按该顺序插入时才是正确的。