1、将Stream转换成数组(Array)
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.IntStream;
public class main {
public static void main(String[] args) {
Stream<String> stringStream = Stream.of("C", "Java", "Python", "cjavapy");
String[] stringArray = stringStream.toArray(size -> new String[size]);
Arrays.stream(stringArray).forEach(System.out::println);
}
}
2、数组与List之间相互转换
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
String[] strArray = new String[]{"C", "Java", "Python", "cjavapy"};
List<String> collect1 = Arrays.stream(strArray).collect(Collectors.toList());
Arrays.stream(collect1.toArray()).forEach(System.out::println);
List<String> arr = Stream.of("C", "Java", "Python", "cjavapy").collect(Collectors.toList());
String[] strArray1 = arr.toArray(new String[arr.size()]);
Arrays.stream(strArray1).forEach(System.out::println);
int[] intArray = new int[]{1, 2, 3,4};
List<Integer> collect2 = Arrays.stream(intArray).boxed().collect(Collectors.toList());
Arrays.stream(collect2.toArray()).forEach(System.out::println);
List<Integer> arr1 = Stream.of(5, 6, 7, 8).collect(Collectors.toList());
Integer[] intArray1 = arr1.toArray(new Integer[arr.size()]);
Arrays.stream(intArray1).forEach(System.out::println);
int[][] ints = new int[][]{{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
List<List<Integer>> collect = Arrays.stream(ints)
.map(o -> Arrays.stream(o).boxed().collect(Collectors.toList()))
.collect(Collectors.toList());
Arrays.stream(collect.toArray()).forEach(System.out::println);
List<List> arrValue = Arrays.asList(Arrays.asList(1,2,3), Arrays.asList(4,5,6), Arrays.asList(7,8,9));
Arrays.stream(arrValue.toArray()).forEach(System.out::println);
List<?> collect5 = arrValue.stream()
.flatMap(list -> (Stream<?>) list.stream())
.collect(Collectors.toList());
Arrays.stream(collect5.toArray()).forEach(System.out::println);
System.exit(0);
}
}
3、不同对象的List转换
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Person p1 = new Person(1, "C", "student");
Person p2 = new Person(2, "Java", "teacher");
Person p3 = new Person(3, "Python", "student");
List<Person> pList = Arrays.asList(p1, p2, p3);
List<Student> sList = pList.stream().filter(p -> p.getPersonType().equals("student"))
.map(p -> new Student(p.getId(), p.getName()))
.collect(Collectors.toList());
sList.forEach(e -> System.out.println("Id:"+ e.getId()+ ", Name: "+ e.getName()));
}
}
class Person {
private int id;
private String name;
private String personType;
public Person(int id, String name, String personType) {
this.id = id;
this.name = name;
this.personType = personType;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getPersonType() {
return personType;
}
}
class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}