简体   繁体   中英

How to iterate over a map to get all the values for 2 keys

I have read csv and stored the values in map (key-value format) I need to fetch all values for 2 particular key into an array list to comapre it with database values columns

ArrayList ftlVal = new ArrayList<>();

                for (Map<String, String> map : finResult) {
                      Object[] key = {"Column2", "Column6"};
                      for (int i = 0; i < key.length; i++) {
                             do {
                                   //for (int j = 0; j <key.length; j++) {
                                          //while (map.get(key[j]) != null) {
                                                Object value = map.get(key[i]);
                                                // System.out.println(value);

                                                if (!ftlVal.contains(value)) {
                                                       ftlVal.add((String) value);
                                                       System.out.println(ftlVal);
                                                       i++;
                                                }
                                          }while (map.containsKey(key[i]));
                                   }
                             }
                             // System.out.println(finResult);
                      //}
                //}

I need all the values for column2 and column6 in an arrylist and then to compare those columns with database column2 and column6

Keeping aside how you compare the ArrayLists with the database columns, the following code will give you the two ArrayLists that you need.

ArrayList<String> column2List = new ArrayList<>();
ArrayList<String> column6List = new ArrayList<>();
for (Map<String, String> map : finResult) {
  String column2Value = map.get("Column2", null);
  String column6Value = map.get("Column6", null);
  column2List.add(column2Value);
  column6List.add(column6Value);
}

The and are the two ArrayLists that you need. 是您需要的两个ArrayLists。

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