老是用到Map转list的方法,可总是记不住,想想还是自己写下来吧,这样好点。
这样就不容易忘了。
public static void testMapVoid () {
Map map = new HashMap();
map.put("a", "a1");
map.put("b", "b1");
map.put("c", "c1");
List listKey = new ArrayList();
List listValue = new ArrayList();
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
String key = it.next().toString();
listKey.add(key);
listValue.add(map.get(key));
}
System.out.println("Convert Finished !");
for(int i =0 ;i<listKey.size();i++){
System.out.print("Key :"+listKey.get(i));
System.out.println(" Value :"+listValue.get(i));
}
}
这样就不容易忘了。
本文提供了一个实用的Java代码示例,展示了如何将Map集合转换为List集合,并分别保存键和值。此方法适用于需要从Map中提取键值对进行进一步处理的场景。
2468

被折叠的 条评论
为什么被折叠?



