How to convert a list object to a Map and iterate through Map
Let us consider we have a list object named usersList, now our requirement is to convert this list object into map and iterate through the map.
Each List object is nothing but a row in the database table
We can understand this better with an example
for (Map<String, Object> map : usersList) {
JSONObject jsonObject = new JSONObject();
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if(“PRODUCT_NAME”.equals(key)){
//product name is name of the coloumn in the database
jsonObject.put(“productName”, value);
}
else if(“CONTRACT_STATUS”.equals(key)){
jsonObject.put(“contractStatus”, value);
}
Here the iteration through the list means iterating through reach row
i.e
List—>L[0]—->Row—>C[0],V[0] (coloumn,Value)
C[1],V[1]
C[3],V[3]
Iterating through map takes place by iterating through each column of database and its corresponding value for that particular row in the list
Map—>M[0]—–>coloumn–>C[0],V[0]
M[1]———————->C[1],V[1]