简体   繁体   English

如何访问源自数据库的ArrayList中的哈希图?

[英]How to access hashmaps inside an ArrayList originated from a database?

I have an ArrayList with a bunch of hashmaps. 我有一个带有一堆哈希图的ArrayList。 Each hashmap has the name and value of a column originated from a database. 每个哈希图都有一个源于数据库的列的名称和值。 How do I access the value of these columns? 如何访问这些列的值?

The Netbeans variable inspector shows me: Netbeans变量检查器向我显示:

avaliacaoList = (java.util.ArrayList) [{id_grade_curricular=5476, posicao=...

And the debugger presents me with the values below: 调试器为我提供以下值:

带有哈希映射的ArrayList

I need to iterate in the ArrayList, accessing each hashmap and his values. 我需要在ArrayList中进行迭代,访问每个哈希图及其值。 I tried many ways to iterate in this ArrayList, but I could not find a way to access the field id_grade_curricular (see orange arrow) of each "record". 我尝试了许多方法来迭代此ArrayList,但是找不到用于访问每个“记录”的字段id_grade_curricular (请参见橙色箭头)的方法。 Can someone help me to achieve this? 有人可以帮助我实现这一目标吗? Thanks. 谢谢。

I am assuming you have a list of hashmaps and that you absolutely do need it that way. 我假设您有一个哈希表列表,并且您确实确实需要这种方式。

for (HashMap h : avaliacaoList){
    Object result = h.get("id_grade_curricular");
    //do something
}

Instead of Object use whatever you are storing. 可以使用您存储的任何内容来代替Object。

Remember, the items in the ArrayList are HashMaps . 记住, ArrayList中的项目是HashMaps You have to then actually access the key:value pair you want. 然后,您必须实际访问所需的key:value对。

for (HashMap entry : avaliacaoList){
    int value = entry.get("id_grade_curricular")
    // Do something with value here
}

Note here I'm assuming the type of the map is HashMap<string,int> since that's what your example looks like. 请注意,这里我假设地图的类型是HashMap<string,int>因为这就是您的示例的样子。 Adjust the types accordingly if that's not true. 如果不正确,请相应地调整类型。

Please don't use Raw Types . 请不要使用原始类型 You could iterate the List and call Map.get() with something like 您可以迭代List并使用类似方法调用Map.get()

List<HashMap<String,String>> avaliacaoList;
for (HashMap<String,String> map : avaliacaoList) {
  System.out.println(map.get("id_grade_curricular"));
}

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

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