streamex
streamex copied to clipboard
How to do a zip operation on 2 EntryStreams?
hi, I have the following pseudo code that I want to rewrite in Stream:
Object[][] array = new Object[allRows.size()][allCols.size()];
for (int i = 0; i < allRows.size(); i++) {
for (int j = 0; j < allCols.size(); j++) {
xxx code;
array[i,j] = xxx();
}
}
The 2 indexes i and j must be used in the loop。
The way I thought of doing it was to replace the 2-fold for loop with zip and iterate over the elements with index by way of EntryStream.of(allRows), but I found that the zip method is not supported by EntryStream, but is supported by the normal StreamEx class, so how do I do it by way of StreamEx? How can I achieve my requirement by means of StreamEx?
I did the zip test with 2 normal Lists and found that it doesn't achieve the effect I want, how can I use StreamEx to replace the above for loop?
List<String> rows = Lists.newArrayList("a", "b", "c");
List<String> cols = Lists.newArrayList("1", "2", "3");
EntryStream.of(rows).flatMap(rowsEntry -> EntryStream.of(cols).flatMap(colsEntry -> Stream.of(Pair.of(rowsEntry, colsEntry))))
.forEach(pair -> {
System.out.println("threadName == " + Thread.currentThread().getName()
+ ",rows.index == " + pair.getLeft().getKey()
+ ",rows.value == " + pair.getLeft().getValue()
+ ",cols.index == " + pair.getRight().getKey()
+ ",cols.value == " + pair.getRight().getValue()
);
});
I myself have achieved
` List<String> rows = Lists.newArrayList("a", "b", "c"); List<String> cols = Lists.newArrayList("1", "2", "3");
StreamEx.of(rows).cross(cols).forKeyValue((key, val) -> {
System.out.println("threadName == " + Thread.currentThread().getName()
+ ",rows.value == " + key
+ ",cols.value == " + val
);
});`
does this work ?
?? my code block
@loordgek Thanks for learning about the cross API, but I need to use the index to iterate over the relevant elements in a loop, which doesn't have an index, and the EntryStream doesn't have a cross method.
why do you need the index cant you use the entrystream?
@loordgek Because at the end of the loop I have to write one of the internally calculated data into a two-dimensional array, the two-dimensional array needs an index, which is the index associated with each element of the loop rows and cols.
@loordgek You can see from my code at the top that at the end I need to write the index and the calculated data to the array, so I need both the elements and the index in the Cartesian product loop
https://gist.github.com/8d5c50bfe7094fb0a573ca4d75d40120
i dont know what to put in the characteristics method
@loordgek The result is incorrect and can be compared to my code below, where the CHARACTERISTICS can be CONCURRENT, as my actual code would use parallel().
List<String> rows = Lists.newArrayList("a", "b", "c");
List<String> cols = Lists.newArrayList("1", "2", "3");
EntryStream.of(rows).flatMap(rowsEntry -> EntryStream.of(cols).flatMap(colsEntry -> Stream.of(Pair.of(rowsEntry, colsEntry))))
.forEach(pair -> {
System.out.println("threadName == " + Thread.currentThread().getName()
+ ",rows.index == " + pair.getLeft().getKey()
+ ",rows.value == " + pair.getLeft().getValue()
+ ",cols.index == " + pair.getRight().getKey()
+ ",cols.value == " + pair.getRight().getValue()
);
});
As I see, no actual feature request is here, and the question was answered. Hence, I close this.