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

查看:30
本文介绍了从 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 的大小显着增加.现在我的问题是如何从数组列表中的两个映射键中获取值?我试过这个

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

附注: -
你的代码在我看来很混乱.我建议,将代码放在一边,再考虑一下您的 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天全站免登陆