I have a single String of format:
row1col1 row1col2
row2col1 row2col2
row3col1 row3col2
and so on...
I want to extract each item and build an array of objects with properties like this:
new MyObject(row1col1, row1col2);
I am new to Java 8 and Streams and I would like to find out how can I achieve this without loops.
Normally I would use a String.split('\n') for accumulating the rows into an array of String
And then a loop where for each line I would split again on the space separator and with the resulting array of the two elements (row1col1 row1col2) build my object, until there are no more rows to process.
Like this:
String sausage = "row1col1 row1col2\nrow2col1 row2col2\nrow3col1 row3col2";
String[] rows = sausage.split("\n");
for (String row : rows) {
String[] objectData = u.split("\\s+");
MyObject myObject = new MyObject(objectData[0], objectData[1]);
myObjectList.add(myObject);
}
Can anyone explain me how to achieve the same with streams and what is the mechanism behind that allows me to do so?
Is this even a valid way of thinking when increasing the number of elements because from all the examples I've seen the streams focus on filtering, collecting or generally given a set of elements retrieve a minor set applying some criterias.