欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

遍历map输出String数组

程序员文章站 2022-03-10 21:01:09
...
public class test01 {
	
	public static void main(String[] args) {
		
    	List<Map<String, Object>> list01 = new ArrayList<Map<String, Object>>();
    	List<Map<String, Object>> list02 = new ArrayList<Map<String, Object>>();
    	Map map01 = new HashMap<String, Object>();
    	map01.put("1","01");
    	list01.add(map01);
    	Map map02 = new HashMap<String, Object>();
    	map02.put("2","02");
    	list01.add(map02);
    	Map map03 = new HashMap<String, Object>();
    	map03.put("3","03");
    	list01.add(map03);
    	Map map04 = new HashMap<String, Object>();
    	map04.put("4","04");
    	list01.add(map04);
    	Map map05 = new HashMap<String, Object>();
    	map05.put("5","05");
    	list02.add(map05);
    	Map map06 = new HashMap<String, Object>();
    	map06.put("6","06");
    	list02.add(map06);
    	Map map07 = new HashMap<String, Object>();
    	map07.put("7",list02);
    	list01.add(map07);
    	
    	String[][] strData = getStringArr(list01);
    	
    	for (int i = 0; i < strData.length; i++) {
    		for (int j = 0; j < strData[i].length; j++) {
    			System.out.println(strData[i][j]);
    		}
    	}
    	
	}
	
	private static String[][] getStringArr(List<Map<String, Object>> list) {
		String strResult = "";
    	String[] strArrSplit = listToStringArr(list, strResult).split(">");
    	String[][] strData = new String[strArrSplit.length][];
    	for (int i = 0; i < strArrSplit.length; i++) {
    		strData[i] = strArrSplit[i].split(",");
    	}
    
    	return strData;
	}
	
	private static String listToStringArr(List<Map<String, Object>> list, String strResult) {
    	
    	Map<String, Object> map = new HashMap<String, Object>();
    	
    	for (int i = 0; i < list.size(); i++) {
    		map = (Map<String, Object>)list.get(i);
    		Set<String> setMap = map.keySet();
    		Iterator<String> it = setMap.iterator();
    		while(it.hasNext()) {
    			String strKey = it.next();
    			Object strValue = map.get(strKey);
    			if (strValue instanceof List) {
    				listToStringArr((List<Map<String, Object>>)strValue, strResult);
    			} else {
    				strResult += strKey + "," + String.valueOf(strValue) + ">";
    			}
    		}
    	}
    	
    	return strResult;
    }
}
相关标签: map