Skip to content
Advertisement

Filter distinct groups and get average of String values parsing as Integer or Double

I have a POJO like below:

JavaScript

And a List like below:

JavaScript

Now I am trying to filter and group all the employees by department and need to get the highest spending department.

If salary is of type Double (say) I can do it like below:

JavaScript

And I get the expected output:

JavaScript

But I am clueless what to do if I get the salary as String and have to parse it as Double using Java 8’s stream chaining?

Advertisement

Answer

You can parse it while averaging. Try using this lambda:

JavaScript

instead of this method reference:

JavaScript

The full pipeline:

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