Java 根据HashMap Key Value获取ArrayList索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18653308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get ArrayList Index according to HashMap Key Value
提问by Samadhan Medge
I Have a ArrayList as ArrayList<HashMap<String, String>> arrayList_LatLong = new ArrayList<HashMap<String, String>>();
in which I put HashMap into that ArrayList. The HashMap key-value pair suppose as follows
我有一个 ArrayList ArrayList<HashMap<String, String>> arrayList_LatLong = new ArrayList<HashMap<String, String>>();
,因为我将 HashMap 放入该 ArrayList。HashMap 键值对假设如下
map.put(key1,value1);
map.put(key2,value2);
map.put(key3,value3);
map.put(key4,value4);
map.put(key5,value5);
Now I want ArrayList Index according to Value1, that means I have Value1 and I want the respective ArrayList Index so that I can get Other Values also from the respective HashMap.
现在我想要根据 Value1 的 ArrayList 索引,这意味着我有 Value1 并且我想要相应的 ArrayList 索引,以便我也可以从相应的 HashMap 中获取其他值。
回答by Antoine Marques
I think you just need to defined it this way :
我认为你只需要这样定义它:
ArrayList<HashMap<String, Integer>> arrayList_LatLong = new ArrayList<HashMap<String, Integer>>();
When you want to get the index in the list you just gotta do :
当您想获取列表中的索引时,您只需执行以下操作:
Integer newIndex = arrayList_LatLong.get(someIndex).get("aKey");
HashMap<String, Integer> indexes = arrayList_LatLong.get(newIndex);
回答by Mr Roshan Pawar
try this--->
试试这个--->
public static int getIndexOFValue(String value, List<Map<String, String>> listMap) {
int i = 0;
for (Map<String, String> map : listMap) {
if (map.containsValue(value)) {
return i;
}
i++;
}
return -1;
}
回答by user2746424
You can do something like:
您可以执行以下操作:
Here i am searching for the value "android". you can change it according to your code
在这里,我正在搜索值“android”。你可以根据你的代码改变它
String value = "android";
int getIndex(){
int i = -1;
for (Map<String, String> map : arrayList_LatLong) {
for (String key : map.keySet()) {
if (map.get(key).equals(value)) {
i = arrayList_LatLong.indexOf(map);
}
}
}
return i;
}
回答by Artem
public static int getIndexOFkey(String key, ArrayList> listMap) {
public static int getIndexOFkey(String key, ArrayList> listMap) {
int i = 0;
for (i=0; i<listMap.size(); i++)
{
if(listMap.get(i).get("id").equalsIgnoreCase(key))
{
return i;
}
}
return -1;
}