Java集合间互相转换
1. List转数组
- 通过toArray()推荐
public static void main(String[] args) {
//1. 通过 toArray()
List<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add("value" + i);
}
String[] arrays = list.toArray(new String[0]);
System.out.println(Arrays.toString(arrays));
}
- jdk1.8 stream
public static void main(String[] args) {
//2. jdk1.8 stream
List<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add("value" + i);
}
String[] arrays = list.stream().toArray(String[]::new);
System.out.println(Arrays.toString(arrays));
}
2.数组转List
- Arrays.asList()
public static void main(String[] args) {
/*
* 此种方法生成的List不可进行add和remove操作
* 因为它是一个定长的List集合,跟数组长度一致
*/
String[] array = new String[]{"value1", "value2", "value3"};
List<String> stringList = Arrays.asList(array);
System.out.println(stringList.toString());
}
- 通过Collections.addAll(list, arrays)
public static void main(String[] args) {
//2.通过Collections.addAll(list, arrays);
List<String> stringList=new ArrayList<>();
String[] array=new String[]{"value1","value2","value3"};
Collections.addAll(stringList, array);
System.out.println(stringList.toString());
}
- jdk1.8 通过Stream
public static void main(String[] args) {
//3. jdk1.8 通过Stream
String[] arrays = new String[]{"value1", "value2", "value3"};
List<String> listStrings = Stream
.of(arrays)
.collect(Collectors.toList());
System.out.println(listStrings.toString());
}
3. Set转数组
- 通过 toArray()
private static void setToArray1() {
Set<String> set = new HashSet<>();
set.add("value1");
set.add("value2");
set.add("value3");
//Set-->数组
String[] array=set.toArray(new String[0]);
System.out.println(Arrays.toString(array));
}
- 通过Stream jdk1.8
private static void setToArray2() {
Set<String> set = new HashSet<>();
set.add("value1");
set.add("value2");
set.add("value3");
//Set-->数组
String[] array= set.stream().toArray(String[]::new);
System.out.println(Arrays.toString(array));
}
4. 数组转Set
-通过先转List之后引入Set
//数组-->Set
private static void arrayToSet() {
String[] array = {"value1","value2","value3"};
Set<String> set = new HashSet<>(Arrays.asList(array));
System.out.println(set);
}
- jdk1.8之后通过Stream
private static void arrayToSet2() {
String[] array = {"value1","value2","value3"};
Set<String> set = Stream.of(array).collect(Collectors.toSet());
System.out.println(set);
}
5. Set转List
- 普遍使用
//set为null会报错
private static void setToList() {
Set<String> set = new HashSet<>();
set.add("value1");
set.add("value2");
set.add("value3");
set=null;
List<String> list=new ArrayList<>(set);
System.out.println(list.toString());
}
- jdk1.8通过Stream
//效率低不推荐
private static void setToList2() {
Set<String> set = new HashSet<>();
set.add("value1");
set.add("value2");
set.add("value3");
List list=Stream.of(set.toArray(new String[0])).collect(Collectors.toList());
System.out.println(list.toString());
}
6. List转Set
//list为null会报错
private static void listToSet() {
List<String> list = new ArrayList<>();
list.add("value1");
list.add("value2");
list.add("value3");
Set<String> set=new HashSet<>(list);
System.out.println(set.toString());
}
7.List转JSONArray
List<T> list = new ArrayList<T>();
JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
8.JSONArray转List
JSONArray array = new JSONArray();
List<EventColAttr> list = JSONObject.parseArray(array.toJSONString(), EventColAttr.class);
9.字符串转List
String str = "";
List<T> list = JSONObject.parseArray(str,T.class);