amaembo / streamex

Enhancing Java Stream API
Apache License 2.0
2.18k stars 249 forks source link

How to do a zip operation on 2 EntryStreams? #259

Closed asasas234 closed 1 year ago

asasas234 commented 2 years ago

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?

asasas234 commented 2 years ago

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?

asasas234 commented 2 years ago
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

loordgek commented 2 years ago

` List rows = Lists.newArrayList("a", "b", "c"); List 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 ?

loordgek commented 2 years ago

?? my code block

asasas234 commented 2 years ago

@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.

loordgek commented 2 years ago

why do you need the index cant you use the entrystream?

asasas234 commented 2 years ago

@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.

asasas234 commented 2 years ago

@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

loordgek commented 2 years ago

https://gist.github.com/8d5c50bfe7094fb0a573ca4d75d40120

loordgek commented 2 years ago

i dont know what to put in the characteristics method

asasas234 commented 2 years ago

@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()
                    );
                });
amaembo commented 1 year ago

As I see, no actual feature request is here, and the question was answered. Hence, I close this.