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 ...
随机推荐
- SignalR的安装
介绍 SignalR 是 ASP.NET 团队正在开发的一个 Microsoft .NET Framework 库和 jQuery 插件,可能包括在以后版本的 ASP.NET 平台中. 它提供了一些前 ...
- 024. asp.net中第一次使用GridView (设置鼠标经过时更换背景色)
1. 前端HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Inde ...
- http://stackoverflow.com/questions/12601907/loading-google-maps-in-anonymous-function
http://stackoverflow.com/questions/12601907/loading-google-maps-in-anonymous-function window.gMaps ...
- git同一文件由于文件名大小写不同导致不能合并
问题 git中如果两个分支添加了同一个文件,但是文件名大小写不同会出现合并问题. 应为git中大小写不同被视为不同文件,但是windows操作系统中不区分文件名大小写.所以在合并是总是没有办法合并. ...
- Centos Mysql 升级
如何升级CentOS 6.5下的MySQL CentOS 6.5自带安装了MySQL 5.1,但5.1有诸多限制,而实际开发中,我们也已经使用MySQL 5.6,这导致部分脚本在MySQL 5.1中执 ...
- Innodb 表修复(转)
摘要: 突然收到MySQL报警,从库的数据库挂了,一直在不停的重启,打开错误日志,发现有张表坏了.innodb表损坏不能通过repair table 等修复myisam的命令操作.现在记录下 ...
- ie数组不支持indexOf 方法解决
if(!Array.prototype.indexOf){ Array.prototype.indexOf = function(obj){ for(var i=0;i<this.length; ...
- 【转】ODBC、OLE DB、 ADO的区别
一.ODBC ODBC的由来 1992年Microsoft和Sybase.Digital共同制定了ODBC标准接口,以单一的ODBC API来存取各种不同的数据库.随后ODBC便获得了许多数据库厂商和 ...
- SQL Server 2008中SQL增强之三:Merge(在一条语句中使用Insert,Update,Delete) 一条语句实现两表同步(添加、删除、修改)
MERGE 目标表 USING 源表 ON 匹配条件 WHEN MATCHED THEN 语句 WHEN NOT MATCHED THEN 语句; http://www.chinaz.com/prog ...
- 【linux】wc命令
Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项][文件] 2.命令参数: -c char统计字节数. ...