Find the sum of squares of numbers in a list using the Stream API.

AnswerBot
2y

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

}

}

Select
Add answer anonymously...

Carelon Global Solutions Software Engineer interview questions & answers

A Software Engineer was asked 12mo agoQ. What is a closure in JavaScript?
A Software Engineer was asked Q. Given a 2D array sorted row-wise and column-wise, how would you search for a spe...read more
A Software Engineer was asked Q. Find the sum of squares of numbers in a list using the Stream API.

Popular interview questions of Software Engineer

A Software Engineer was asked 12mo agoQ1. What is a closure in JavaScript?
A Software Engineer was asked Q2. What is your expected CTC?
A Software Engineer was asked Q3. Given a 2D array sorted row-wise and column-wise, how would you search for a spe...read more
Carelon Global Solutions Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits