Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am new to Java 8 streams, I am unable to get the correct output from the nested loop operational sum.

In the nested loop, I am calling a method which is returning an integer; by this doing finally summing up the resultant total

This is what I tried:

    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.IntStream;

    public class NestedLoop {

        public void callTask(int i, int j) {
            System.out.println("i:"+i+"j:"+j);
        }


        public int getCount(Count count, String j) {
            if(count.getCount() >= 123 && count.getCount() <= 125) {
                System.out.println("###");
                return 1;
            }
            else {
                return 0;
            }
        }



        public static void main(String[] args) {

            List<Count> list2 = new ArrayList<>();
            List<String> list1 = new ArrayList<>();

            Count count = new Count(121);
            Count count1 = new Count(122);
            Count count2 = new Count(123);
            Count count3 = new Count(124);
            Count count4 = new Count(125);
            list2.add(count4);
            list2.add(count2);
            list2.add(count3);
            list2.add(count1);
            list2.add(count);

            NestedLoop loop = new NestedLoop();
            list1.add("list2 - Op1");
            list1.add("list2 - Op2");
            list1.add("list2 - Op2");
            list1.add("list2 - Op2");
            int _count = 0;

          IntStream res = list2.parallelStream().flatMapToInt(x -> IntStream.of(list1.stream().mapToInt(y-> loop.getCount(x,y)).sum()));
          System.out.println(res.sum());
        }
    }

Count class   
    class Count {

        private int count;

        public Count(int count) {
            super();
            this.count = count;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }

    }

By this, I am getting 12 instead of 2.

Help, me where the code is faulty.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
217 views
Welcome To Ask or Share your Answers For Others

1 Answer

Please dry run the code and see first, because 12 is the correct answer. For each element in list2 you iterate list1 and then call the getCount, summing the values. For the value 123 it is called 4 times, same for 124 and 125. Each returns 1, summing them all 1 * 3 * 4 yields 12. Why you need 2? What is the rationale?

Anyway is this what you are asking for? This still yields 3, based on your if statement.

int sum = list2.stream()
        .mapToInt(x -> list1.stream().map(y -> loop.getCount(x, y)).findAny().orElse(0))
        .sum();

However, your existing solution which yields 12, can further be simplified like so,

int sum = list2.stream()
    .flatMapToInt(x -> list1.stream().mapToInt(y -> loop.getCount(x, y)))
    .sum();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...