2014-11-05 98 views
-2

如何获取arrayList中的HashTable的值?ArrayList中的Java HashMap

我有以下代码:

public ArrayList resultSetToArrayList(ResultSet rs) { 

    ArrayList list = new ArrayList(); 
    try { 
     ResultSetMetaData md = rs.getMetaData(); 
     int columns = md.getColumnCount(); 
     int rowcount = 0; 
     while (rs.next()) { 
      Hashtable row = new Hashtable(); 
      for (int i = 1; i <= columns; ++i) { 
       row.put(md.getColumnName(i), rs.getString(i)); 
      } 
      list.add(rowcount, row); 
      rowcount++; 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return list; 
} 
+0

的HashMap!=哈希表,请更新您的标题。 – Pshemo 2014-11-05 02:58:17

回答

3

你似乎是使用raw typesHashtable代替HashMap的。我想你问像

public List<Map<String, String>> resultSetToArrayList(ResultSet rs) { 
    List<Map<String, String>> list = new ArrayList<>(); 
    try { 
     ResultSetMetaData md = rs.getMetaData(); 
     int columns = md.getColumnCount(); 
     while (rs.next()) { 
      Map<String, String> row = new HashMap<>(); 
      for (int i = 1; i <= columns; ++i) { 
       row.put(md.getColumnName(i), rs.getString(i)); 
      } 
      list.add(row); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return list; 
} 

至于获得的值回了Map,你可能会迭代Map.keySet()

for (String key : map.keySet()) { 
    System.out.printf("%s = %s%n", key, map.get(key)); 
} 
+0

好的谢谢,但我怎样才能得到ArrayList中的值的ArrayList? – 2014-11-05 03:11:57

+0

-1 - 建议改进,但实际上并未回答问题。 – immibis 2014-11-05 03:15:23

+0

'list.get(rowNumber).get(fieldName)''? – msandiford 2014-11-05 03:16:41