in Education by
I have a List list=Arrays.asList(640,480,520,170,320,140,60); And I need to find sum for given elements as per below iteration logic. For example first Outer Iteration like 640+480, Then 640+480+520 and so on till 60. Next iteration starts from 480+520 , then 480+520 +170 and so on. The sample Java 7 Program is like List list=Arrays.asList(640,480,520,170,320,140,60); List newListWithSum=new ArrayList<>(); for(int mainIndex=0;mainIndex<list.size();mainIndex++) { for(int index=mainIndex;index<list.size();index++) { int sum=0; for(int nestedIndex=mainIndex;nestedIndex<index+1;nestedIndex++) { sum=sum+list.get(nestedIndex); } newListWithSum.add(sum); } } but I need to change above logic to Java 8 version. Please help/share hint to write a simplified Java 8 Logic for sum as per below iteration JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I feel this is much more similar to algorithm, let's go step by step 1) First get all the sublists by excluding first integer in each iteration List list=Arrays.asList(640,480,520,170,320,140,60); List> re = IntStream.range(0, list.size()) .mapToObj(sb->list.subList(sb, list.size())) .filter(s->s.size()>1) .collect(Collectors.toList()); re.forEach(ls->System.out.println(ls)); Output [640, 480, 520, 170, 320, 140, 60] [480, 520, 170, 320, 140, 60] [520, 170, 320, 140, 60] [170, 320, 140, 60] [320, 140, 60] [140, 60] 2) Now on each list do the sum List> re1 = re.stream() .map(j->IntStream.rangeClosed(2, j.size()).mapToObj(sl->j.stream().limit(sl).mapToInt(Integer::intValue).sum()).collect(Collectors.toList())) .collect(Collectors.toList()); re1.forEach(ls->System.out.println(ls)); Output [1120, 1640, 1810, 2130, 2270, 2330] [1000, 1170, 1490, 1630, 1690] [690, 1010, 1150, 1210] [490, 630, 690] [460, 520] [200] Combined solution of step 1 and step 2 List> re = IntStream.range(0, list.size()) .mapToObj(sb->list.subList(sb, list.size())) .filter(s->s.size()>1) .map(j->IntStream.rangeClosed(2, j.size()).mapToObj(sl->j.stream().limit(sl).mapToInt(Integer::intValue).sum()).collect(Collectors.toList())) .collect(Collectors.toList());

Related questions

0 votes
    Wap in Java to print a pascal triangle in simple language. Please explain the logic Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    write a program in Java to input a number and print if it is a special number or not. do this program using ... nested and while loop . Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    I have an ETL requirement like: I need to fetch around 20000 records from a table and process each ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I asked a question yesterday and none of the answers are working so I decided to start a fresh one ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    I have tried several ways but somehow they dont look clean; I have a URL file in Excel format (400+ urls in a ... easy way to do it? Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I need help on looping all my list and print my list name. Currently, I have a few raspberry pi types and windows PC ... ) print ("\n") Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    The value of the control variables can be decreased in the For …..Next loop with the help of the ___ statement in looping statement Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    Which of the following has a greater benchmark time for looping and JQuery vs core JavaScript in milliseconds ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 21, 2021 in Education by JackTerrance
0 votes
    How can looping be done over a list of hosts in a group, inside of a template?...
asked Jul 29, 2021 in Technology by JackTerrance
0 votes
    I have written the following program in C# to display an ASCII pyramid. public static void method1() { int k; int kCond = 5; int i; int inc = 0; for (int p = 0; p...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I need to create a hollow pyramid with a solid top layer like so: Height: 5 * *** * * * ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    Which of the following ecological food chains does not represent an erect pyramid of numbers? (a) ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 8, 2021 in Education by JackTerrance
0 votes
    An intricate network of food chains is called (a) Biosphere (b) Food web (c) Energy pyramid ... Science,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
...