user17207982
user17207982

Reputation:

Further split array in Java

I have one string array 'oldArray' & want to create a 'newArray' with the elements of 'oldArray' in this way by splitting:

String[] oldArray = {"1|14", "10|15", "16|5"};
newArray after spliting oldArary should be like :

newArray = [1,14,10,15,16,5];

and println the newArray

Upvotes: 0

Views: 35

Answers (1)

user17201277
user17201277

Reputation:

Try this.

public static void main(String[] args) throws IOException {
    String[] oldArray = {"1|14", "10|15", "16|5"};
    String[] newArray = Arrays.stream(oldArray)
        .flatMap(s -> Arrays.stream(s.split("\\|")))
        .toArray(String[]::new);
    System.out.println(Arrays.toString(newArray));
}

output:

[1, 14, 10, 15, 16, 5]

Upvotes: 2

Related Questions