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

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
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);
}
}
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);
}
}
Carelon Global Solutions Software Engineer interview questions & answers
Popular interview questions of Software Engineer


Reviews
Interviews
Salaries
Users

