Looping Within Multiple Arrays in Java Stream

Java 8 stream's forEach with multiple arrays

An indirect approach with a stream of array indices is probably your best bet, given that there is no "zip" operation in the Streams API:

import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;

...

range(0, firstnames.length)
.mapToObj(i-> String.format("firstname: %s\nlastname: %s\nmailaddress: %s\n",
firstnames[i], lastnames[i], mailaddresses[i]))
.collect(toList())
.forEach(System.out::print);

Creating multiple arrays using one for loop in Java

String[] elem1 = reader.readLine().split("\\s+");
String[] elem2 = reader.readLine().split("\\s+");
for(int i = 0; i < size; i++)
{
arr1[i] = Integer.parseInt(elem1[i]);
arr2[i] = Integer.parseInt(elem2[i]);
}

Java - loop over multiple arrays (String does not match "array type")

As @Andres stated in his answer, the only way is reflection, although only for member variables.

One solution would be adding your arrays to another array and then looping over it.

static String[] E1 = {"filex1", "file333y", "readme"};
//...
static String[][] es = {E1, E...};

Convert nested for loop logic to for each and java stream

The solution to this task comes from the definition.

  • If list of lists should be converted to plain List, Stream::flatMap is used
  • If it is needed to skip list elements at indexes 0, then Stream::skip(1) should be used.
  • If no duplicate elements are needed, then Stream::distinct should be applied.
  • If some values need to be added to the resulting list, Collectors.collectingAndThen may be used along with the IntStream.range to calculate the indexes where "Heading" needs to be inserted.

To sum it up, an example implementation can look like:

public static final List<String> formatData(List<List<String>> data, String header, int afterN) {
return data.stream().skip(1) // skip i == 0
.flatMap(lst -> lst.stream().skip(1)) // skip j == 0, make flat list
.distinct() // remove duplicates
.collect(Collectors.collectingAndThen(
Collectors.toList(),
lst -> IntStream.range(0, lst.size()).boxed()
.flatMap(i -> i % afterN == 0
? Stream.of(header, lst.get(i))
: Stream.of(lst.get(i))
)
.collect(Collectors.toList())
));
}

Test

List<List<String>> data = Arrays.asList(
Arrays.asList("Skipped line"),
Arrays.asList("Skipped column 1", "column1", "column2", "column3"),
Arrays.asList("Skipped column 2", "value 2.1", "value 2.2", "value 2.3"),
Arrays.asList("Skipped column 3", "value 3.1", "value 3.2", "value 3.3"),
Arrays.asList("Skipped column 4", "value 4.1", "value 4.2", "value 4.3"),
Arrays.asList("Skipped column 5", "value 5.1", "value 5.2", "value 5.3", "value 5.3"),
Arrays.asList("Skipped column 6", "6.1", "6.2", "6.3", "value 5.1", "value 4.2"),
Arrays.asList("Skipped column 7", "7.1", "7.2", "7.3", "7.1", "7.3"),
Arrays.asList("Skipped column 8", "8.1", "8.2", "8.3", "8.4", "8.4", "8.5")
);

// use \n to improve output, insert header after each 3 elements
List<String> finalData = formatData(data, "\nHeader", 3);

System.out.println(finalData);

Output (without duplicate elements)

[
Header, column1, column2, column3,
Header, value 2.1, value 2.2, value 2.3,
Header, value 3.1, value 3.2, value 3.3,
Header, value 4.1, value 4.2, value 4.3,
Header, value 5.1, value 5.2, value 5.3,
Header, 6.1, 6.2, 6.3,
Header, 7.1, 7.2, 7.3,
Header, 8.1, 8.2, 8.3,
Header, 8.4, 8.5]

How to Loop and Print 2D array using Java 8

Keeping the same output as your for loops:

Stream.of(names)
.flatMap(Stream::of)
.forEach(System.out::println);

(See Stream#flatMap.)

Also something like:

Arrays.stream(names)
.map(a -> String.join(" ", a))
.forEach(System.out::println);

Which produces output like:

Sam Smith
Robert Delgro
James Gosling

(See String#join.)

Also:

System.out.println(
Arrays.stream(names)
.map(a -> String.join(" ", a))
.collect(Collectors.joining(", "))
);

Which produces output like:

Sam Smith, Robert Delgro, James Gosling

(See Collectors#joining.)

Joining is one of the less discussed but still wonderful new features of Java 8.

How to traverse multiple list using Java 8 stream?

This works when the lists' size are same or different:

List<Double> list1 = List.of(1D, 1.5D);
List<Double> list2 = List.of(30D, 25D);
List<Double> list3 = List.of(30D, 25D);
Stream<List<Double>> listStream = Stream.of(list1, list2, list3);

int maxSize = listStream.mapToInt(List::size).max().orElse(0);

IntStream.range(0, maxSize)
.forEach(index -> {
listStream
.filter(list -> list.size() > index)
.forEach(list -> System.out.print(list.get(index) + " "));
System.out.println();
});


Related Topics



Leave a reply



Submit