List<Map<String, Object>>使用总结

List<Map<String, Object>>
List集合中的对象是一个Map对象,而这个Map对象的键是String类型,值是Object类型。

package com.huahua.test;

import java.util.*;

public class TestListMap {
    public static void main(String[] args) {

        List<Map<String, Object>> listMaps = new ArrayList<Map<String, Object>>();

        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("1", "a");
        map1.put("2", "b");
        map1.put("3", "c");
        listMaps.add(map1);

        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("11", "aa");
        map2.put("22", "bb");
        map2.put("33", "cc");
        listMaps.add(map2);
        
        //遍历listMaps 集合的三种方式
        for (Map<String, Object> map : listMaps) {
            for (String s : map.keySet()) {
                System.out.print(map.get(s) + "  ");
            }
        }
        System.out.println();
        System.out.println("========================");
        for (int i = 0; i < listMaps.size(); i++) {
            Map<String, Object> map = listMaps.get(i);
            Iterator iterator = map.keySet().iterator();
            while (iterator.hasNext()) {
                String string = (String) iterator.next();
                System.out.println(map.get(string));
            }
        }
        System.out.println("++++++++++++++++++++++++++++");
        for (Map<String, Object> map : listMaps) {
            for (Map.Entry<String, Object> m : map.entrySet()) {
                System.out.print(m.getKey() + "    ");
                System.out.println(m.getValue());
            }
        }
        System.out.println("-----------------------------");
    }
}

项目中使用举例

String sql = "select v.order_num,v.empadname,v.emp_name,v.status,v.assigned_to,v.create_time,v.update_time  from view_pu89_selfhelp v where v.materielno = '"+materielNo+"' and v.purchaseorder = '"+purchaseOrder+"' and v.sapprojectnum = '"+projectNum+"'";
List<Object> params = new ArrayList<>();
List<Map<String, Object>> datas = executeJDBCSqlQuery(sql, params);

for (Map<String, Object> map : datas) {
         for (Map.Entry<String, Object> m : map.entrySet()) {
             System.out.print(m.getKey() + "    ");
             System.out.println(m.getValue());
         }
     }

public List executeJDBCSqlQuery(String sql, List<Object> params) {
	SQLQuery rs = getSession().createSQLQuery(sql);
	rs.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
	if (!params.isEmpty()) {
		for (int i = 0; i < params.size(); i++) {
			rs.setParameter(i, params.get(i));
		}
	}
	return rs.list();
}

datas查询出的数据如下

[{ORDER_NUM=PU8920210810001, EMP_NAME=test1, STATUS=采购员确认, EMPADNAME=SZ600218, ASSIGNED_TO=张三丰918, CREATE_TIME=2021-08-10 11:23:10, UPDATE_TIME=2021-08-12 09:19:38}]

你可能感兴趣的:(Java,java,iterator)