Assuming that the list of lists is called matrix
, the following code snippet demonstrates how to perform a row-first traversal using IntStream:
int numRows = matrix.size();
int numCols = matrix.get(0).size();
IntStream.range(0, numRows)
.flatMap(i -> IntStream.range(0, numCols).map(j -> matrix.get(i).get(j)))
.forEach(value -> System.out.print(value + " "));
Here's how this works:
IntStream
of row indices using IntStream.range(0, numRows)
.i
, we create an inner IntStream
over the column indices using IntStream.range(0, numCols)
.IntStream
, we use map
to retrieve the element at (i, j)
in the matrix, where j
is the column index.flatMap
so that we get a single stream of all the values in the matrix.forEach
, printing each value to the console.Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-03-13 11:00:00 +0000
Seen: 14 times
Last updated: Oct 14 '22
What is the procedure for utilizing backup and restoration methods for all databases in mongodb?
What is the procedure for utilizing Firebase phone authentication on Expo with React Native in 2022?
What is the procedure to remove all markers and layers from a Leaflet map in order to add new ones?
What is the procedure for streamlining a lambda expression that involves counting?
What is the procedure for ending a local server when using the Parcel bundler?
What is the procedure for replacing missing data by the average of a particular category?