map转换成list
Java代码如下:
package Test01; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class Test05 { // map转换成list @SuppressWarnings({ "unchecked", "rawtypes" }) public static List mapTransitionList(Map map) { List list = new ArrayList(); Iterator iter = map.entrySet().iterator(); // 获得map的Iterator while (iter.hasNext()) { Entry entry = (Entry) iter.next(); list.add(entry.getKey()); list.add(entry.getValue()); } return list; } @SuppressWarnings("unchecked") public static void main(String[] args) { @SuppressWarnings("rawtypes") Map map = new HashMap(); map.put("aaa", 11); map.put("bbb", "22ss"); map.put("ccc", "汉字"); @SuppressWarnings("rawtypes") List list = mapTransitionList(map); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } } }
代码运行后效果如下: