从ArrayList Java中的HashMap键中检索所有值 [英] Retrieve all values from HashMap keys in an ArrayList Java

查看:86
本文介绍了从ArrayList Java中的HashMap键中检索所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,这现在让我感到困惑(大脑冻结!),似乎缺少了一些东西.有一个我用HashMap填充的ArrayList.现在我放入我的HashMap和arraylist.

Good day, this is kind of confusing me now(brain freeze!) and seem to be missing something. Have an ArrayList which i populate with a HashMap. now i put in my HashMap and arraylist.

 Map.put(DATE, value1);
 Map.put(VALUE, value2);

 arraylist.put(Map);

由于正在解析JSON,因此arraylist的大小显着增加.现在我的问题是如何从arraylist中的两个映射键中获取值?我已经尝试过了

Since am parsing a JSON, the arraylist increases in significant size. now my question is how do you get the values from both map keys in the arraylist? i have tried this

  if(!list.isEmpty()){   // list is an ArrayList

            for(int k = 0; k < list.size(); k++){
                map = (HashMap)list.get(k);
            }

        }

        Log.d(TAG, "map size is" + map.size());

        String [] keys = new String[map.size()];
        String [] date_value = new String[map.size()];

        String [] value_values = new String[map.size()];

        int i = 0;
        Set entries = map.entrySet();
        Iterator iterator = entries.iterator();

        while(iterator.hasNext()){

            Map.Entry mapping = (Map.Entry)iterator.next();
            keys[i] = mapping.getKey().toString(); 
            date_value[i] = map.get(keys[i]);

            if(keys[i].equals(DATE)){
                date_value[i] = map.get(keys[i]);

            } else if(keys[i].equals(VALUE)){
                value_values[i] = map.get(keys[i]);
            }

                   i++;
                 }

但是我似乎无法获得所有的价值. Map大小始终返回值2,这只是元素.我如何从ArrayList的Map键中获取所有值?谢谢

But i can't seem to get all the values. the Map size always return a value of 2, which is just the elements. how can i get all the values from the Map keys in the ArrayList? Thanks

推荐答案

当您已有工作要做时,为什么要重新发明轮子. Map.keySet()方法为您提供了Map中所有键的Set.

Why do you want to re-invent the wheel, when you already have something to do your work. Map.keySet() method gives you a Set of all the keys in the Map.

Map<String, Integer> map = new HashMap<String, Integer>();

for (String key: map.keySet()) {
    System.out.println("key : " + key);
    System.out.println("value : " + map.get(key));
}

此外,您的第一个for循环对我来说很奇怪:-

Also, your 1st for-loop looks odd to me: -

   for(int k = 0; k < list.size(); k++){
            map = (HashMap)list.get(k);
   }

您正在遍历列表,并将每个元素分配给相同的reference - map,这将覆盖所有先前的值.您将拥有的只是列表中的最后一个映射.

You are iterating over your list, and assigning each element to the same reference - map, which will overwrite all the previous values.. All you will be having is the last map in your list.

编辑:-

如果您想要地图的键和值,也可以使用entrySet.那对您来说更好:-

You can also use entrySet if you want both key and value for your map. That would be better bet for you: -

    Map<String, Integer> map = new HashMap<String, Integer>();

    for(Entry<String, Integer> entry: map.entrySet()) {
        System.out.println(entry.getKey());
        System.out.println(entry.getValue());
    }

PS :-
您的代码对我来说很混乱.我建议保留该代码,然后再考虑您的design.就目前而言,如代码所示,很难理解其意图.

P.S.: -
Your code looks jumbled to me. I would suggest, keep that code aside, and think about your design one more time. For now, as the code stands, it is very difficult to understand what its trying to do.

这篇关于从ArrayList Java中的HashMap键中检索所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆