如何从HashMap列表中获取值?

时间:2011-06-15 13:57:20

标签: java collections map arraylist hashmap

我有List HashMapInteger类型的密钥Long,类型为List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>(); for (int i = 0; i < 10; i++) { HashMap<Integer, Long> mMap = new HashMap<Integer, Long>(); mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i)); ListofHash.add(mMap); }

HashMap

现在,如何从Collection

列表中检索密钥和值

如果使用public static List<Map<Integer, Long>> populateMyHashMapFromDB() { List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>(); for (int i = 0; i < 10; i++) { HashMap<Integer, Long> mMap = new HashMap<Integer, Long>(); mMap.put(Integer.valueOf(getIntFromDB(i)), Long.valueOf(getLongFromDB(i))); ListofHash.add(mMap); } return ListofHash; } 类是解决方案,请点击我。

更新1:

实际上我从数据库中获取值并将其放入HashMap

public void getmyDataBaseValues()
{
List<HashMap<Integer,Long>> ListofHash=populateMyHashMapFromDB();

// This is where i got confused

}

其中getIntFromDB和getLongFromDB可以分别检索任何int和long值。

现在这就是我想要获得我从数据库获得的值和值

的值
public class HashMaps {


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        testPrintHashmapValues(putToHashMap());
        testPrintHashmapValuesWithList(putToHashMapWithList());
    }

    public static Map<Integer, Long> putToHashMap() {
        Map<Integer, Long> map = new LinkedHashMap<Integer, Long>();
        for (int i = 0; i < 10; i++) {
            map.put(Integer.valueOf(i), Long.valueOf(200000000000L + i));
        }
        return map;
    }

    public static void testPrintHashmapValues(Map<Integer, Long> map) {
        for (Map.Entry<Integer, Long> entry : map.entrySet()) {
            System.out.println("key: " + entry.getKey() + " value: "
                    + entry.getValue());
        }
    }

    public static List<Map<Integer, Long>> putToHashMapWithList() {
        List<Map<Integer, Long>> listOfHash = new ArrayList<Map<Integer, Long>>();
        for (int i = 0; i < 10; i++) {
            Map<Integer, Long> mMap = new HashMap<Integer, Long>();

            mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L + i));
            listOfHash.add(mMap);
        }
        return listOfHash;
    }

    public static void testPrintHashmapValuesWithList(
            List<Map<Integer, Long>> listOfHash) {

        for (int i = 0; i < 10; i++) {
            Map<Integer, Long> map = listOfHash.get(i);
            for (Map.Entry<Integer, Long> entry : map.entrySet()) {
                System.out.println(i + " hashMap");
                System.out.println("key: " + entry.getKey() + " value: "
                        + entry.getValue());
            }
        }
    }

}

答案 这就是Peter Lawrey建议的原因

{{1}}

即使没有创建列表,我的任务也已完成。

6 个答案:

答案 0 :(得分:2)

您遍历列表以获取地图,然后迭代其键集:

public static void main(String[] args) throws NoSuchAlgorithmException {
  List<Map<Integer, Long>> ListofHash = new ArrayList<Map<Integer,Long>>();
  for (int i = 0; i < 10; i++) {
    Map<Integer, Long> mMap = new HashMap<Integer, Long>();
    mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L + i));
    ListofHash.add(mMap);
  }

  for (Map<Integer, Long> map : ListofHash) {
    for (Integer key : map.keySet()) {
      System.out.println(map.get(key));       
    }
  }
}

注意:在可能的情况下,我还使用Map代替HashMap更改了一些代码

答案 1 :(得分:2)

我假设您正在询问如何从ArrayList获取它,

ListofHash.get(0).get(Object key);

对象键表示您想要的任何整数键

祝你好运!

答案 2 :(得分:2)

我仍然不清楚为什么你想要一个清单。

public static Map<Integer, Long> populateMyHashMapFromDB() {
    Map<Integer, Long> map = new LinkedHashMap<Integer, Long>();
    for (int i = 0; i < 10; i++) 
            map.put(getIntFromDB(i), getLongFromDB(i));
    return map;
}

Map<Integer, Long> map = populateMyHashMapFromDB();
Long value = map.get(key);

此集合并非旨在轻松为您提供键/值对。我需要这个功能,我建议使用不同的结构。

假设你有一些不好的代码你无法改变,你可以做

List<Map<Integer, Long>> maps = new ArrayList<Map<Integer, Long>>();

Map<Integer, Long> all = new HashMap<Integer, Long>();
for(Map<Integer, Long> map: maps)
    all.putAll(map);

for(Map.Entry<Integer, Long> entry: all.entrySet() {
    // do something which each key/value.
}

在此示例中,您不需要列表或地图。

long[] longs = new long[10];
for (int i = 0; i < 10; i++) 
    longs[i] = i;

int key = 1;
int num = longs[key];

答案 3 :(得分:1)

这种效果:

public static Long getValue(Integer key)) {

    for (HashMap<Integer, Long> entry : ListofHash) {
        if (entry.containsKey(key)) {
            return entry.get(key);
        }
    }

    return null;
}

//Retrieve all key/value
for (HashMap<Integer, Long> entry : ListofHash) {
    for (Integer key : entry.keySet()) {
        System.out.println("Key : " + key + ", value: " + entry.get(key));
    }
}

PS 未经测试。

答案 4 :(得分:1)

List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>();
        for (int i = 0; i < 10; i++) {
            HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
            mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i));
            ListofHash.add(mMap);
        }

答案 5 :(得分:1)

您总共有10个地图,只存储了一个元素。这些地图存储在列表中。您可以检索这些地图及其元素:

public void testPrintHashmapValues()  {
    List<HashMap<Integer, Long>> listOfHash = new ArrayList<HashMap<Integer, Long>>();
    for (int i = 0; i < 10; i++) {
        HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
        mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i));
        listOfHash.add(mMap);
    }

    for (int i = 0; i < 10; i++) {
        Map<Integer, Long> map = listOfHash.get(i);
        for (Map.Entry<Integer, Long> entry : map.entrySet()) {
            System.out.println(i + " hashMap");
            System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
        }
    }

}

此外,变量不应以大写( ListOfHash )开头。尽管java不会抱怨它是一种不好的做法,并且难以阅读这样的代码,因为有人可能认为它是类名而不是变量。

相关问题