简体   繁体   English

如何使用Java 8流生成2D int数组?

[英]How to generate a 2D int array using Java 8 stream?

I am trying to generate a int[][] using Java 8 streams. 我正在尝试使用Java 8流生成int[][]

This is what I have done so far: 这是我到目前为止所做的:

objects.parallelStream()
            .map(o -> o.getPropertyOnes()
                    .parallelStream()
                    .map(t-> t.getIndex())  //<-- getIndex() returns int
                    .mapToInt(i -> i)
                    .toArray())            //<-- here I have a Stream<int[]>
            .toArray();                   //lost here

At the end of outer .map() I have a Stream<int[]> , but not sure how to convert that to int[][] . 在外部.map()的末尾,我有一个Stream<int[]> ,但不知道如何将其转换为int[][] Please suggest. 请建议。

First you can simplify the map().mapToInt() to mapToInt(t-> t.getIndex()) (maybe you should use a method reference like <type>::getIndex ). 首先,您可以将map().mapToInt()简化为mapToInt(t-> t.getIndex()) (也许您应该使用类似<type>::getIndex的方法引用)。

As you said you have Stream<int[]> after the map stage. 如你所说,你在map阶段后有Stream<int[]> Then you only need to provide an array generator function like: 那么你只需要提供一个数组生成器函数,如:

int[][] array = Stream.of(1, 2, 3, 4)
                      .map(i -> IntStream.range(0, i).toArray())
                      .toArray(int[][]::new);

Output: 输出:

[[0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]

You need a toArray(generator) method that helps us to specify a return type by an IntFunction<T[]> function: 你需要一个toArray(generator)方法来帮助我们通过IntFunction<T[]>函数指定一个返回类型:

int[][] a = Stream.of(new int[]{1, 2, 3}, new int[]{4, 5, 6}).toArray(int[][]::new);

instead of toArray() , which returns Object[] regardless of an incoming type in a Stream (invokes toArray(Object[]::new) internally): 而不是toArray() ,它返回Object[]而不管Stream中的传入类型(在内部调用toArray(Object[]::new) ):

Object[] a = Stream.of(new int[]{1, 2, 3}, new int[]{4, 5, 6}).toArray();

If you are interested in, behind the scenes, all of this have the following look: 如果您对幕后感兴趣,所有这些都具有以下外观:

  1. forming a Node [an ArrayNode in our case] (an immutable container for keeping an ordered sequence of elements) from a Pipeline of the previous stage; 形成一个Node [在我们的例子中是一个ArrayNode ](一个用于保持有序元素序列的不可变容器)来自前一阶段的Pipeline ;
  2. array size calculation by using the node size ( node.count() ); 使用节点大小计算数组大小( node.count() );
  3. getting a new empty array with a needed length by our IntFunction<T[]> generator ( i -> new int[i][] or simply int[][]::new ); 通过我们的IntFunction<T[]>生成器获取一个具有所需长度的新空数组( i -> new int[i][]或简单地int[][]::new );
  4. copying from the node to this array and returning the latter. 从节点复制到此数组并返回后者。

If you want to generate 2D array of sequential number of n X n where n in the degree. 如果要生成序列号为n X n的二维数组,其中n为度。

int n = 4;
System.out.println(Arrays.deepToString(IntStream.rangeClosed(0, n - 1)
        .boxed().map(x -> IntStream.rangeClosed(x * n + 1, (x + 1) * n)
            .boxed().toArray()).toArray()));

Output: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM