Reduce Code Verbosity in Java 8 by Using Functional Expressions

One of the most common criticisms of Java is that it is too verbose. Java 8 finally introduced some functional programming features such as lambda expressions and a stream API for the collection classes.

Functional expressions tend to be declarative; they tell you what they do, not how. Therefore they are usually more powerful and succint. They should be a great tool to fight Java's infamous verbosity and make the code more pleasant to the eye. Still not all is as good as it seems. This is how one would create a list comprehension in Java.

 squares = numbers.stream().map (a -> a*a).collect (Collectors.toList());  

Compare that with the following Haskell expression and notice the stark contrast in simplicity and elegance.

 squares = [x*x | x <- numbers]  

It is a good idea to hide this unsightly abomination in the guts of a method in a custom utility class in a utility project that we link to from every other Java project. If you normally use maven, create a repository just for functional helper methods and add it to your pom file every time.

Here is an example of how to wrap the expression above in a method and end up with a much cleaner call:

https://stackoverflow.com/a/26206464/1045902

 public static <T, R> List<R> apply(Collection<T> coll, Function<? super T, ? extends R> mapper) {  
   return coll.stream().map(mapper).collect(Collectors.toList());  
 }  
 List<Integer> squares = apply(numbers, a -> a * a);  

Here is another example taken from my own code.

   /**  
   * check if the points lay all on the same plane  
   */  
   public static boolean inSamePlane(List<Point3D> points) {  
    Preconditions.checkNotNull(points);  
    if(points.size() <= 2) {  
      throw new IllegalArgumentException("Too few points");  
    }  
    //For efficiency check first cases where the plane is perpendicular to one of the axes
    boolean allEqualForSomeCoordinate =   
       ! isThereDifferent(points, Point3D::getX) || ! isThereDifferent(points, Point3D::getY) || ! isThereDifferent(points, Point3D::getZ);  
      
    if(allEqualForSomeCoordinate)  
      return true;  
   
    //Check the rest of the cases using the plane equation  
    //...  
      
    return true;  
   }  
   
   private static boolean isThereDifferent(List<Point3D> points, Function<Point3D, Double> getACoordinate) {  
    double firstVal = getACoordinate.apply(points.get(0)).doubleValue();  
    return points.parallelStream().anyMatch(p->(different(getACoordinate.apply(p).doubleValue(), firstVal)));  
   }  
   
   private static boolean different(double d1, double d2) {  
    return !GeometryUtil.almostEqual(d1, d2);  
   }  

Hopefully there is no need for an explanation of what this code does. I created it with readability in mind.

Once you understand it, take a look at the highlighted statement and the two private static methods. I am using streams and functions as an argument, two new features in Java 8. For its conciseness, it doesn't lack readibility. If it were done without these features we'd need three loops, also involving code repetition.

Comments