join int array to create big array

vaydesalme :

I have a condition that I need to create a big array by joining small arrays. I am using the code

int[] types = {4,7,9,2,1,7};
int arr[] = IntStream.range(0, list.size()).map(x -> types).collect();

I need the copy of types that should be the number of size of the list. I can do it with simple loop with the conditions, but is it possible with lambda expressions?

Thanks

Eran :

You can use flatMap:

int arr[] = IntStream.range(0, list.size())
                     .flatMap(x -> IntStream.of(types))
                     .toArray();

This will create an IntStream of list.size() copies of the types array and then collect it to a single array.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=18668&siteId=1