Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
asasas234 opened this issue Mar 15, 2022 · 12 comments
Closed

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

asasas234 opened this issue Mar 15, 2022 · 12 comments

Comments

@asasas234
Copy link

asasas234 commented Mar 15, 2022

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
Copy link
Author

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
Copy link
Author

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
Copy link

` 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
Copy link

?? my code block

@asasas234
Copy link
Author

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

@asasas234 asasas234 reopened this Mar 16, 2022
@loordgek
Copy link

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

@asasas234
Copy link
Author

@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
Copy link
Author

@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
Copy link

@loordgek
Copy link

i dont know what to put in the characteristics method

@asasas234
Copy link
Author

@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
Copy link
Owner

amaembo commented Sep 24, 2022

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

@amaembo amaembo closed this as completed Sep 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants