简体   繁体   中英

Iterating over values in a HashMap stored within an ArrayList

Bit of a beginner's questions but...

I have a ResultSet object that I return from a database - 3 columns of 30 rows.

I retrieve the following dataset:

在此处输入图片说明

using the following:

   try {
        preparedStatement = conn.prepareStatement(
                sqlStatement,
                ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);

        rs = preparedStatement.executeQuery();

        ResultSetMetaData md = rs.getMetaData();
        int columns = md.getColumnCount();

        while(rs.next()) {
            HashMap row = new HashMap(columns);
            for(int i=1; i<=columns; ++i){
                row.put(md.getColumnName(i),rs.getObject(i));
            }
            list.add(row);
        }

...and I return it as an ArrayList

在此处输入图片说明

...which, when debugging, displays as follows:

在此处输入图片说明

I want to be able to iterate over the HashMap values, in pseudo code:

  1. get key 0
  2. get value 0, 1 & 2
  3. use these as test data
  4. if no luck then try key 2

This shouldn't be hard I know, I'm just struggling to find the best way of iterating over an ArrayList of HashMaps

You are using raw types, and your IDE or compiler are throwing you tons of warnings about this. You should heed them. Becaus you didn't, you were capable of writing code that assigns a list containing maps (each map representing one returned db result, mapping column names to the values in the row)... to a variable of type List<String> .

This model of translating a DB row to a map is a bad idea. There are plenty of nice libraries out there to interact with DBs. For example, JDBI is simple to understand has a more thought-through API for accessing results. It can even map results onto java data types.

If you must use the model you've pasted so far, for starters, add generics everywhere so that the compiler would have marked that down as a compile-time error. At the very least make that List<Map<String, Object>> .

Let me reiterate that switching things over to JDBI is a far superior road forward, but in the interests of answering the directly posed question, once you've added the generics and fixed your variable types, you can do this:

List<Map<String, Object>> data = getResultSet(syndicatorName);
for (int rowIdx = 0; rowIdx < data.size(); i++) {
    Map<String, Object> row = data.get(rowIdx);
    for (Map.Entry<String, Object> cell : row.entrySet()) {
        System.out.printf("Row %d cell %s: %s\n", rowIdx, cell.getKey(), cell.getValue());
    }
}

Note that your cell values are 'Object' here. You'd have to cast them to what you know the data type to be. This is not idiomatic java but there's no way to fix that without completely redesigning the getResultSet method. Again.. JDBI or similar libraries is what you really want here.

You need a nested Loop (at least i would do it that way...there are not only a few other ways):

for (HashMap<T,T> i : ArrayList<HashMap<T,T>>) {
    for (T j : i.keySet()){
        i.get(j);
        //further code
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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