不多废话,直接看代码,有注解
public class Map转List {
public static void main(String[] args) {
//开辟空间
HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
//存入数据
hashMap.put(1,"张三");
hashMap.put(2,"李四");
hashMap.put(3,"王五");
//使用Collection类型接收HashMap的Value值
Collection<String> collection = hashMap.values();
//把Collection对象作为参数传入ArrayList构造方法完成类型转换
ArrayList<String> arrayList = new ArrayList<String>(collection);
//输出测试
System.out.println(arrayList.toString());
}
}
将HashMap转换为ArrayList:Java实用技巧
本文介绍如何通过Java代码将HashMap中的字符串值转换为ArrayList,展示了如何使用values()方法获取值集合并传递给ArrayList构造函数。
2437

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



