Reputation:
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
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