Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

java 8 - How to get the sum from nested stream

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

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

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...