Coding Question 1 - Find sum of squares of numbers in list using stream API.

AnswerBot
1y

Find sum of squares of numbers in list using stream API.

  • Use mapToInt() to convert list of integers to IntStream

  • Use map() to square each element of IntStream

  • Use sum() to get the sum of squares

Anonymous
1y

import java.util.*;

public class Main {

public static void main(String[] args) {

List<Integer> nums = Arrays.asList(1,2,3,4);

System.out.println("original list - " + nums);

int num_square = nums.stream() //converted list into stream

.mapToInt(Integer::intValue) //converted Integer into int

.map(n -> n*n) // squaring each number using lambda exp

.sum(); // doing sum of squares

System.out.println(num_square);

}

}

piyush bhadade
1y

import java.util.*;

public class Main {

public static void main(String[] args) {

List<Integer> nums = Arrays.asList(1,2,3);

System.out.println("original list - " + nums);

int num_square = nums.stream() //converted list into stream

.mapToInt(Integer::intValue) //converted Integer into int

.map(n -> n*n) // squaring each number using lambda exp

.sum(); // doing sum of squares

System.out.println(num_square);

}

}

Add answer anonymously...
Carelon Global Solutions Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter