How to get the sum from nested stream

喜你入骨 提交于 2020-01-07 08:48:38

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/50888506/how-to-get-the-sum-from-nested-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!