Java Map,List,Set,String[]之间的转换

Map和List

map.values转list:

Collection values = resultMap.values();
        ArrayList resultList = new ArrayList(values);
 
 

Map和set

map转set(主要用于迭代遍历)

map.keySet();
map.entrySet();

set转map就是遍历添加暂无提供简便方法

List和Set(一般是用来去重)

list转set

List list = new ArrayList<>();
HashSet toSet = new HashSet(list);

set转List

list.addAll(toSet);

List和String[]

list转String[]

List list = new ArrayList<>();
String[] string = new String[list.size()];
list.toArray(string);

String[]转list

String[] string = new String[]{"data"};
List list = Arrays.asList(string);

Set和String[]

set转string[]

Set set = new HashSet();
set.add("data");
String[] string = set.toArray(new String[set.size()]);

string[]转set(其实是set和list的转换)

String[] string = new String[]{"data"};
Set set = new HashSet(Arrays.asList(string));

你可能感兴趣的:(Java Map,List,Set,String[]之间的转换)