1

//I am looking for loop using streams as follows:

int inputArray[] = {4, 6, 5, -10, 8, 1, 2};

    for (int i : inputArray)
            {
                for (int j = i+1; j < inputArray.length; j++)
                {
                    if(inputArray[i]+inputArray[j] == inputNumber)}}

// Tried something like but its not working

IntStream intStream1 =  Arrays.stream(inputArray);

    intStream1.forEach(i -> {
            IntStream.range(i+1,... ).forEach(j -> {

            });
        });
4
  • 1
    Your first example doesn't make sense, inputArray[-9] isn't going to work (when i is -10 for example). Commented Feb 9, 2018 at 3:15
  • 1
    Not clear what you are trying to achive. You should at least finish this statement: if(inputArray[i]+inputArray[j] == inputNumber) Commented Feb 9, 2018 at 3:20
  • It seems that the problem statement should be: Find any two numbers in an array which sum to a given value. OP is trying to create all pairs; first by nested loops (but got that wrong), and then by its streams equivalent Commented Feb 9, 2018 at 3:28
  • what are you trying to do if you and explain what you want to do in words, it could easy to help you. Commented Feb 9, 2018 at 3:29

2 Answers 2

2

in your expression you iterate through the values of the array, so for

for (int i: inputArray)

values of i will be: 4, 6, 5, -10, 8, 1, 2 sequentially.

Judging by the style of your code, you need

for (int i = 0; i < inputArray.length; i++) 

or similar.

Same story with streams. If you create a primitive stream out of an array of integers, foreach would iterate over its values, not indexes. So, you would need to account for indexes yourself. Here is a possible implementation.

private static int i = 0, j= 0;
IntStream iStream = Arrays.stream(inputArray);

iStream.forEachOrdered(ival -> {
    j = i+1;
    if (j < inputArray.length) {
        IntStream jStream = Arrays.stream(inputArray, j, inputArray.length);      
        jStream.forEachOrdered(jval -> {
            System.out.println(i + "+" + j + ": " + (ival  + jval));
            j++;
        });
    }
    i++;
});

I believe that the above would explain the main idea with streams.

you can also use IntStram.range() to iterate across indexes themselves, or use other possible streaming solutions. Please see other answers in this thread as well.

Sign up to request clarification or add additional context in comments.

2 Comments

Op is asking for streams answer
@IanMc thanks, i added some info to my answer as well.
1

Here is the streams answer you are asking for (finding out if any two numbers in an array sum to a given value)

int inputArray[] = {4, 6, 5, -10, 8, 1, 2};
int inputNumber = -8;

IntStream.range(0, inputArray.length-1).forEach(i -> IntStream.range(i+1, inputArray.length)
    .forEach(j -> {
        if (inputArray[i]+inputArray[j] == inputNumber) 
            System.out.println("Found "+inputNumber+"; a["+i+"]="+inputArray[i]+" a["+j+"]="+inputArray[j]);
        }));;

Which produces the result:

Found -8; a[3]=-10 a[6]=2

2 Comments

his problem statement is not clear, but since he has i+1 inside the second loop, the first loop would be IntStream.range(0, inputArray.length -1) ?
It is very clear that the OP wants to add all pairs. This is typical algorithm question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.