How to optimize java code using the features of java 8 The code below works fine but I want to optimize it and write it in another way.