Home > Blockchain >  How to clone an array using Java stream?
How to clone an array using Java stream?

Time:05-22

I have the following array:

int nums[] = new int[]{4,5,6,7,0,1,2};

I want to add the same elements to the array using stream or another proper way if it is better using stream. There is addAll() method for List, but I could not use it for array and I am not sure if there is a proper way for array. So, how can I convert this list to:

int nums[] = new int[]{1,2,3,4,5,1,2,3,4,5};

CodePudding user response:

Based desired output, i'll make a guess that initial array is:

int[] nums = new int[]{1, 2, 3, 4, 5};

You can't change the size of an array, once created, so you need to declare a new one. @Dan has already offered a solution with streams, so i'll give one with loops for completeness sake.

public class Temp {

    public static void main(String[] args) throws Exception {
        int[] nums = new int[]{1, 2, 3, 4, 5};
        System.out.println("Initial array");
        System.out.println(Arrays.toString(nums));

        int[] result = new int[nums.length * 2];
        for (int i = 0; i < nums.length; i  ) {
            result[i] = nums[i];
            result[i   nums.length] = nums[i];
        }

        System.out.println("Result array");
        System.out.println(Arrays.toString(result));
    }
}

The upside is the single iteration used to fill result array.

Edit: As per suggestion from @OleV.V. a solution using System.arraycopy() would look like this:

public class Temp {

    public static void main(String[] args) throws Exception {
        int[] nums = new int[]{1, 2, 3, 4, 5};

        int[] result = new int[nums.length * 2];
        System.arraycopy(nums, 0, result, 0, nums.length);
        System.arraycopy(nums, 0, result, nums.length, nums.length);
        
        System.out.println(Arrays.toString(result));
    }
}

The upside is that System.arraycopy() is highly optimised and efficient.

CodePudding user response:

Basing on the output you've shown int nums[] = new int[]{1,2,3,4,5,1,2,3,4,5}; and your statement

I want to add the same elements to the array

I think what you were trying to ask was how to re-add the elements of an array to the same array, although your first snippet showed int nums[] = new int[]{4,5,6,7,0,1,2}; while the output was totally unrelated to the first array (might have been a small blunder).

However, in Java it is not possible to update the size of an array. Once an array has been instantiated its size is fixed. What you can do is to declare another array with double the size of the original array you want to copy from.

As it has been pointed out in the comments by @Ole V.V. you could achieve this with IntStream, which is a better approach than the one I've used since it doesn't force you to change your working type from int[] to Integer[]. In fact, the Stream class works with generic types, so the only argument types it works with are references; therefore you could only retrieve an array of Integer[] rather than int[].

Solution with IntStream

int nums[] = new int[]{1, 2, 3, 4, 5};

int[] res = IntStream.concat(IntStream.of(nums), IntStream.of(nums)).toArray();

System.out.println(Arrays.toString(res));

Solution with Stream

int nums[] = new int[]{1, 2, 3, 4, 5};

Integer[] res = new Integer[0];
res = Stream.concat(Arrays.stream(nums).boxed(), Arrays.stream(nums).boxed())
        .collect(Collectors.toList())
        .toArray(res);

System.out.println(Arrays.toString(res));

CodePudding user response:

Iterative or stream based solutions are unnecessarily complicated and slow. Just use System.arraycopy:

public class Test {
    public static void main(String[] args) {

        int[] a = {1,2,3,4,5};
        int[] b = {6,7,8,9,10};

        int[] aAndB = new int[a.length   b.length];
        System.arraycopy(a, 0, aAndB, 0, a.length);
        System.arraycopy(b, 0, aAndB, a.length, b.length);

        System.out.println(Arrays.toString(aAndB));
    }
}

System.arraycopy is highly optimised: Why is System.arraycopy native in Java?

CodePudding user response:

how about using Arrays.copyOf(arg, arg)?

public static void main(String[] args) {
 int[] nums = new int[]{1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
 int[] copiedArray = Arrays.copyOf(nums, nums.length);
}

If you really want to use Stream,

public static void main(String[] args) {
 int[] nums = new int[]{1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
 int[] copiedArray = Arrays.stream(nums).toArray();
}

CodePudding user response:

use Arrays.asList(your array) to convert your array to list

List<Integer> numList = Arrays.asList(nums);
  • Related