高维数组扁平化操作

package com.dj.springboot.study.demo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: ldj
 * @Date: 2024/01/29/18:21
 * @Description: 高维数组扁平化
 */
public class ListDemo {

    public static void main(String[] args) {
        List> list = new ArrayList<>();
        list.add(Arrays.asList(1,2,3));
        list.add( Arrays.asList(4,5,6));

        //[[1, 2, 3], [4, 5, 6]]
        System.out.println(list);

        //平铺 [1, 2, 3, 4, 5, 6]
        System.out.println(list.stream().flatMap(List::stream).collect(Collectors.toList()));
    }
}

你可能感兴趣的:(java,java)