简体   繁体   English

如何从HashMap列表中获取值?

[英]How to Get Values from List of HashMap?

I have a List of HashMap 's which has key of type Integer and value of type Long . 我有一个HashMap List ,其中包含Integer类型的键和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);
        }

Now, how do I retrieve the key and value from the list of HashMap ? 现在,如何从HashMap列表中检索键和值?

If using Collection class is the solution, please enlight me. 如果使用Collection类是解决方案,请关注我。

Update 1: 更新1:

Actually i am getting the value from the database and putting that into a HashMap 实际上我从数据库中获取值并将其放入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;


}

where getIntFromDB and getLongFromDB can retreive any int and long values respectively. 其中getIntFromDB和getLongFromDB可以分别检索任何int和long值。

Now this is where i want to get my values that i got from DB both keys and values 现在这就是我想要获得我从DB获得的值和键值的地方

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

// This is where i got confused

}

ANSWER This is why Peter Lawrey suggested 答案这就是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());
            }
        }
    }

}

My Task is done even without creating a List. 即使没有创建List,我的任务也完成了。

You iterate over the list to get the maps, and then iterate over their key set: 您遍历列表以获取映射,然后迭代其键集:

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

Note: I've also changed a little your code, using Map instead of HashMap when possible 注意:我也改变了一些代码,尽可能使用Map而不是HashMap

I am assuming you are asking how to get it from ArrayList, 我假设你问的是如何从ArrayList获取它,

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

Object key means whatever Integer key you want 对象键表示您想要的任何Integer键

Good Luck! 祝好运!

Its still not clear to me why you want a list. 它仍然不清楚为什么你想要一个清单。

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

This collection isn't designed to give you the key/values pairs easily. 此集合不是为了方便地为您提供键/值对。 I fyou need to this functionality I would suggest using a different structure. 我需要这个功能,我建议使用不同的结构。

Assuming you have a some bad code you cannot change, you can do 假设你有一些不好的代码你无法改变,你可以做到

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

In this example you don't need a List or a Map. 在此示例中,您不需要List或Map。

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

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

Something of this effect: 这种效果的东西:

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 Untested. PS未经测试。

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

You have a total of 10 Maps with only one element stored. 您总共有10个地图,只存储了一个元素。 And those Maps are stored in a List. 这些地图存储在列表中。 You can retrieve those Maps and its elements doing this: 您可以检索这些地图及其元素:

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

}

Also the variables should not start with uppercase ( ListOfHash ). 此外,变量不应以大写( ListOfHash开头 Even though java won't complain it is a bad practice and difficult to read code like that because one might think it's a class name instead of a variable. 尽管java不会抱怨它是一种不好的做法,并且难以阅读这样的代码,因为人们可能认为它是类名而不是变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM