一、數組與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));
}
二、數組與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));
}
三、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));
}
四、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));
}
五、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));
}
六、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));
}