list,set,map,数组之间的相互转换详细解析
1.list转set
Set set = new HashSet(new ArrayList());
2.set转list
List list = new ArrayList(new HashSet());
3.数组转为list
List stooges = Arrays.asList("Larry", "Moe", "Curly");
或者
String[] arr = {"1", "2"};
List list = Arrays.asList(arr);
4.数组转为set
int[] a = { 1, 2, 3 };
Set set = new HashSet(Arrays.asList(a));
5.map的相关操作。
Map map = new HashMap();
map.put("1", "a");
map.put('2', 'b');
map.put('3', 'c');
System.out.println(map);
// 输出所有的值
System.out.println(map.keySet());
// 输出所有的键
System.out.println(map.values());
// 将map的值转化为List
List list = new ArrayList(map.values());
System.out.println(list);
// 将map的值转化为Set
Set set = new HashSet(map.values());
System.out.println(set);
6.list转数组
List list = Arrays.asList("a","b");
String[] arr = (String[])list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arr));
//1.数组转换为list,map
String[] arr = {"123","456","789","123"};
List<String> list = Arrays.asList(arr);
//数组转换为map,一维数组时,奇数为key,偶数为value,奇数个元素,最后一个舍掉
//.二维数组当做两个一维数组
Map map = MapUtils.putAll(new HashMap(), arr);
String[][] ss = {{"a","b","e"},{"c","d","f"}};
MapUtils.debugPrint(System.out,arr,map);
MapUtils.putAll(map, arr);
MapUtils.debugPrint(System.out,arr,map);
MapUtils.putAll(map, ss);
MapUtils.verbosePrint(System.out,arr,MapUtils.invertMap(map));
//list 转换为arr
List<String> ls = new ArrayList<String>();
ls.add("wch");
ls.add("name");
String[] as = (String[]) ls.toArray();
/**
* List<Map<Object,Object>> to 二维数组
* @param list
* @param keyLength - Map中的key个数
* @return
*/
public static Object[][] list2array(List<Map<Object,Object>> list,int keyLength){
Object[][] array = new Object[list.size()][keyLength];
for(int i=0;i<list.size();i++){
array[i] = list.get(i).values().toArray();
}
return array;
}
list,set,map,数组之间的相互转换详细解析的更多相关文章
- list,set,map,数组间的相互转换
1.list转set Set set = new HashSet( new ArrayList()); 2.set转list List list = new ArrayList( new ...
- c++中string类对象和字符数组之间的相互转换
string类在c++中是一个模板类,位于名字空间std中,注意这里不是string.h,string.h是C字符串头文件. 将string类型转换为字符数组char arr[10];string s ...
- Java中字符串和byte数组之间的相互转换
1.将字符转换成byte数组 String str = "罗长"; byte[] sb = str.getBytes(); 2.将byte数组转换成字符 byte[] b={(by ...
- 【python】列表与数组之间的相互转换
安装numpy pip3 install numpy 列表转数组 np.array() import numpy as np a = [1, 2, 3] b = np.array(a) 列表转数组 a ...
- C#中List〈string〉和string[]数组之间的相互转换
1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...
- List<string>和string[]数组之间的相互转换,需要的朋友可以参考下
1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...
- c#不同数组之间的转换【转载,消化自动删除】
c#中从string数组转换到int数组 string[] input = { "1", "2", "3", "4", ...
- JavaScriptES6中Map与对象、数组,JSON之间的相互转换
JavaScriptES6中Map与对象.数组,JSON之间的相互转换 https://blog.csdn.net/c__dreamer/article/details/82183130
- Json数组操作小记 及 JSON对象和字符串之间的相互转换
[{"productid":"1","sortindex":"2"},{"productid":&q ...
随机推荐
- Python合并两个numpy矩阵
numpy是Python用来科学计算的一个非常重要的库,numpy主要用来处理一些矩阵对象,可以说numpy让Python有了Matlab的味道. 实际的应用中,矩阵的合并是一个经常发生的操作,如何利 ...
- 使用Ef时,对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性。
EntityValidationErrors 关于如何查看 EntityValidationErrors 详细信息的解决方法 我们在 EF 的编程中,有时候会遇到这样一个错误: 但是,按照他的提示 ...
- 【转】object标签和embed标签
我们现在大部分人做网页,都是直接用DW插入flash,而且DW也是所见即所得,直接生成了相应的flash显示代码.可是我们又有多少人了解这些直接由DW生成的代码呢?其实我接触flash player标 ...
- JavaScript 类定义常用方法(转)
1.对象直接量 var obj1 = { v1 : "", get_v1 : function() { return this.v1; }, set_v1 : function(v ...
- .NET_RSA加密全接触(重、难点解析)
.NET_RSA加密全接触(重.难点解析) .NET Framework提供了两个类供我们使用RSA算法,分别是:用于加密数据的RSACryptoServiceProvider和用于数字签名的DSAC ...
- SSH_框架整合5--验证用户名是否可用
SSH_框架整合5--验证用户名是否可用 1 emp-input.jsp中编写ajax验证用户名是否可用: <script type="text/javascript" SR ...
- MySQL时间戳和时间格式转换函数
MySQL时间戳和时间格式转换函数:unix_timestamp and from_unixtime unix_timestamp将时间转化成时间戳格式.from_unixtime将时间戳转化成时间格 ...
- golang一个深复制的库
https://github.com/mitchellh/copystructure
- JavaScript中Call()以及Apply()的应用
apply()和call()的真正用武之地是能够扩充函数赖以运行的作用域 三点说明: 1.每个函数都包含两个非继承而来的方法:apply()和call(). 2.他们的用途相同,都是在特定的作用域中调 ...
- 黄聪:WordPress动作钩子函数add_action()、do_action()源码解析
WordPress常用两种钩子,过滤钩子和动作钩子.过滤钩子相关函数及源码分析在上篇文章中完成,本篇主要分析动作钩子源码. 然而,在了解了动作钩子的源码后你会发现,动作钩子核心代码竟然跟过滤钩子差不多 ...