JAVA中list,set,map与数组之间的转换详解

package test;

import java.util.*;

/**
* Created by ming
*/
public class Test { public static void main(String[] args) {
Set<Long> longSet=new HashSet<>();
longSet.add(1L);
longSet.add(2L);
List<Long> longList=new ArrayList<>();
longList.addAll(longSet);//list转set
for(Long t : longList){
System.out.println(t);
}
Set<Long> set2=new HashSet<>();
set2.addAll(longList);//list转set
for(Long s : set2){
System.out.println(s);
}
Map<Long,Long> map=new HashMap<>();//list转map
for(Long t : longList){
map.put(t,t);
}
for(Long s : set2){//set转map
System.out.println(s);
} Collection<Long> collections= map.values();
for (Long t : collections){
System.out.println("map转集合"+t);
} Long longs[]={2L,3L,4L};//定义一个数组
List<Long> longList1 = Arrays.asList(longs);//数组转集合
for(Long t : longList1){
System.out.println("数组转集合"+t);
} Long [] newLongs= longList1.toArray(new Long []{});
System.out.println("集合转数组的方法:"+newLongs.length); }
}
上一篇:腾讯安全反病毒实验室解读“Wannacry”勒索软件


下一篇:配置管理之PackageProvider接口