遍历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() 函数
下一篇: 微信小程序页面返回传值的4种解决方案汇总
推荐阅读
-
JavaScript遍历数组的三种方法map、forEach与filter实例详解
-
js/jquery遍历对象和数组的方法分析【forEach,map与each方法】
-
详解vue数组遍历方法forEach和map的原理解析和实际应用
-
Vue指令v-for遍历输出JavaScript数组及json对象的常见方式小结
-
ES6-新增的数组操作,数组解构,forEach,fillter,some.map的数组遍历,数组转换字符串
-
js对比for、forEach、map遍历数组速度
-
php FLEA中二叉树数组的遍历输出
-
PHP中遍历二维数组_以不同形式的输出操作实例
-
vue使用map代替Aarry数组循环遍历的方法
-
mybatis循环遍历map中list、string、实体对象