Using Java 8 find the sum of squares of all the odd numbers in the 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
1y

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

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