如何从HashMap列表中获取值?

Ads*_*Ads 5 java collections arraylist hashmap map

我有一个Listof HashMap,它具有 type 的键和 type 的IntegerLong

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);
        }
Run Code Online (Sandbox Code Playgroud)

现在,如何从 的列表中检索键和值HashMap

如果使用Collection类是解决方案,请启发我。

更新 1:

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

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;


}
Run Code Online (Sandbox Code Playgroud)

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

现在这是我想获取我从数据库获得的值的地方,包括键和值

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

// This is where i got confused

}
Run Code Online (Sandbox Code Playgroud)

ANSWER这就是为什么Peter Lawrey建议

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

}
Run Code Online (Sandbox Code Playgroud)

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

Pet*_*rey 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);
Run Code Online (Sandbox Code Playgroud)

该集合并不是为了轻松地为您提供键/值对而设计的。如果您需要此功能,我建议您使用不同的结构。

假设您有一些无法更改的错误代码,您可以这样做

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.
}
Run Code Online (Sandbox Code Playgroud)

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

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

int key = 1;
int num = longs[key];
Run Code Online (Sandbox Code Playgroud)