Java Stream 数组Array及列表(List)相互转换的方法

Stream是Java 8的新特性,基于lambda表达式,是对集合对象功能的增强,它专注于对集合对象进行各种高效、方便聚合操作或者大批量的数据操作,提高了编程效率和代码可读性。本文主要介绍Java Stream相关的数组Array及列表(List)相互转换的方法,以及相关的示例代码。

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) {
    //数组转List
    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转数组
    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);
    
     //数组转List
    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转数组
    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);
    
    //二维数组转换成List
    //int[][]与List< List >互转
    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);
    
    //ist< List >展平为List
    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); //success
  }
}

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;
    }
} 

推荐阅读
cjavapy编程之路首页