数组与list、set、map相互转换
2019独角兽企业重金招聘Python工程师标准>>>
1、数组与list转换
@Test
public void array2List() {
String[] strArray = { "aaa", "bbb", "ccc" };
List<String> strList = new ArrayList<>();
CollectionUtils.addAll(strList, strArray);
logger.info("strList:{}",JSON.toJSONString(strList));
}
@Test
public void array2List2() {
String[] strArray = { "aaa", "bbb", "ccc" };
List<String> strList =Arrays.asList(strArray);
logger.info("strList:{}",JSON.toJSONString(strList));
}
2、数组与set转换
@Test
public void array2Set() {
String[] strArray = { "aaa", "bbb", "ccc" };
Set<String> strSet = new HashSet<>();
CollectionUtils.addAll(strSet, strArray);
logger.info("strSet:{}",JSON.toJSONString(strSet));
}
@Test
public void array2Set2() {
String[] strArray = { "aaa", "bbb", "ccc" };
Set<String> strSet = new HashSet<>(Arrays.asList(strArray));
logger.info("strSet:{}",JSON.toJSONString(strSet));
}
3、List与Set转换
@Test
public void list2Set(){
String[] strArray = { "aaa", "bbb", "ccc" };
List<String>list=Arrays.asList(strArray);
Set<String> strSet = new HashSet<>(list);
logger.info("strSet:{}",JSON.toJSONString(strSet));
}
4、Set与List转换
@Test
public void set2List(){
String[] strArray = { "aaa", "bbb", "ccc" };
Set<String> strSet = new HashSet<>(Arrays.asList(strArray));
List<String>strList=new ArrayList<>(strSet);
logger.info("strList:{}",JSON.toJSONString(strList));
}
5、Map与Set转换
@Test
public void map2Set(){
Map<String,String>map=new HashMap<>();
map.put("aa", "bb");
map.put("cc", "dd");
Set<String> strSet=map.keySet();
logger.info("strSet:{}",JSON.toJSONString(strSet));
Set<String> strSet2=new HashSet<>(map.values());
logger.info("strSet2:{}",JSON.toJSONString(strSet2));
}
6、Map与List转换
@Test
public void map2List(){
Map<String,String>map=new HashMap<>();
map.put("aa", "bb");
map.put("cc", "dd");
List<String> strList=new ArrayList<>(map.keySet());
logger.info("strList:{}",JSON.toJSONString(strList));
List<String> strList2=new ArrayList<>(map.values());
logger.info("strList2:{}",JSON.toJSONString(strList2));
}
转载于//my.oschina.net/u/182501/blog/1486848
还没有评论,来说两句吧...