如何从HashMap列表中获取值?

5

我有一个List,其中包含多个HashMap,它们的键是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);
        }

现在,如何从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;


}

getIntFromDB和getLongFromDB可以分别检索任何整数和长整数值。

现在,我想获取从数据库中获取的键和值。

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

// This is where i got confused

}
答案这就是为什么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());
            }
        }
    }

}

我的任务已经完成,甚至不需要创建列表。
6个回答

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

另一个改进是使用Google Guava来声明列表:List<Map<Integer,Long>> listOfHash = newArrayList(); - Kevin

2

以下是需要翻译的内容:

大致意思如下:

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

提示 未经测试。


2

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

该内容与IT技术无关。
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.
}

在这个例子中,您不需要使用List或Map。

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

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

1
请问使用Map和HashMap有什么区别?前者的复杂度有多高?(只是好奇想知道) - Ads
1
Map 可以被任何 Map 实现所替代。除非你需要使用仅在 HashMap 上可用的方法,否则最好使用接口。使用 List<Map> 比使用单个 Map 更复杂,正如我在示例中所展示的那样,不清楚为什么需要一个它们的 List。 - Peter Lawrey
是的,我同意在这个例子中不需要哈希表,但在我的情况下,我从数据库中获取键和值,其中值不是递增的,我们也不知道值是什么。请查看我的更新1代码。希望现在已经很清楚了。 - Ads
1
@Adhavan,我已经更新了我的答案。也许你正在试图把事情变得比它们需要的更加复杂。 - Peter Lawrey
1
是的...你说得对...我刚刚和列表混淆了...它可以仅使用HashMap完成... - Ads

2
我假设你正在询问如何从ArrayList中获取它。
ListofHash.get(0).get(Object key);

对象键(Object key)意味着您想要的任何整数键。

祝好运!


这意味着您要查找的内容在第一个地图中(并且存在第一个地图)。 - Peter Lawrey
@Peter,完全正确!我认为他的问题更普遍,关于如何访问这些元素。然而,在ListofHash上进行迭代将是一个更好的选择。 - TeaCupApp
在我看来,他似乎只需要一个 long[10],这将会更简单。 - Peter Lawrey
似乎我必须知道正确的键才能获得值。但是由于我从数据库获取这两者,所以我也不知道键。请参考更新1。 - Ads

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

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不会抱怨,但这是一种不好的做法,很难阅读这样的代码,因为有人可能会认为它是一个类名而不是一个变量。


抱歉!!!打完后有点懒得改了……你发现了,谢谢!!! ;) - Ads

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接