Using Java 8, how would you find the sum of squares of all the odd numbers in an ArrayList?

AnswerBot
1y
Using Java 8, find the sum of squares of all the odd numbers in the arraylist.
Use the stream() method to convert the ArrayList to a stream
Use the filter() method to filter out the even numbers
Use the ...read more
AKSHAY POTDAR
2y
use Java 8 Stream API to find the sum of squares of all the odd numbers in an ArrayList
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
int sumOfOddSquares = numbers.stream()
.filter(n -> n % 2 != 0) // Filter out even numbers
.mapToInt(n -> n * n) // Square each odd number
.sum(); // Sum the squared values
System.out.println(sumOfOddSquares); // Output: 165
Help your peers!
Add answer anonymously...
Wissen Technology Java Developer interview questions & answers
A Java Developer was asked 1w agoQ. What is the purpose of introducing static and default methods in functional inte...read more
A Java Developer was asked 1w agoQ. What are the features introduced in Java 8?
A Java Developer was asked 1w agoQ. What is a functional interface?
Popular interview questions of Java Developer
A Java Developer was asked 1w agoQ1. What is a functional interface?
A Java Developer was asked 1w agoQ2. Given a 2D matrix, how can you perform a spiral traversal to obtain a 1D array?
A Java Developer was asked 1w agoQ3. What are the features introduced in Java 8?
>
Wissen Technology Java Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

