Skip to content
Advertisement

Calculate the percentage of value using Collection framework

I have List of TrainingRequest where each and every element has List of Feedback.

JavaScript

I need to get all given result of Q1,Q2 and calculate percentage of each value.

JavaScript

To flat all the feedback

JavaScript

To calculate each value of Q1 and Q2, I’m grouping it and getting the count. I need to get the percentage of each Q1, Q2 value insted of count.

JavaScript

When I use Collectors.counting(), it’s giving the following output:

JavaScript

But I need it to give percentage as I expected

JavaScript

How to do it in a efficient way? Do I need to optimize the above code?

Advertisement

Answer

This might not be an optimized answer but you can get the result. Create a map to keep total values for each Q, and then use it to calculate percentage,

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement