【问题标题】:Get ArrayList Index according to HashMap Key Value根据HashMap键值获取ArrayList索引
【发布时间】:2013-09-06 08:31:11
【问题描述】:

我有一个 ArrayList<HashMap<String, String>> arrayList_LatLong = new ArrayList<HashMap<String, String>>(); 的 ArrayList,我将 HashMap 放入该 ArrayList。 HashMap 键值对假设如下

map.put(key1,value1);  
map.put(key2,value2);  
map.put(key3,value3);  
map.put(key4,value4);  
map.put(key5,value5);  

现在我想要根据 Value1 的 ArrayList 索引,这意味着我有 Value1 并且我想要相应的 ArrayList 索引,这样我也可以从相应的 HashMap 中获取其他值。

【问题讨论】:

  • 举例说明..

标签: java android arraylist hashmap


【解决方案1】:

试试这个--->

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;
}

【讨论】:

    【解决方案2】:

    我认为你只需要这样定义它:

    ArrayList<HashMap<String, Integer>> arrayList_LatLong = new ArrayList<HashMap<String, Integer>>();
    

    当你想获取列表中的索引时,你只需要这样做:

    Integer newIndex = arrayList_LatLong.get(someIndex).get("aKey");
    HashMap<String, Integer> indexes = arrayList_LatLong.get(newIndex);
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      我在这里搜索值“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;
      }
      

      【讨论】:

        【解决方案4】:

        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;
        }
        

        【讨论】:

        • 缩进第一行并注释一下代码怎么样?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-16
        • 1970-01-01
        • 2011-02-10
        • 2010-12-19
        • 2011-04-27
        相关资源
        最近更新 更多