Functional Programming in Java 8 with Functions

In general terms, functional programming is a programming paradigm, and it’s about programming with functions. What functional programming may be opposed to is the imperative programming paradigm. In imperative programming style, programs are composed from elements that “do” something. “Doing” something generally implies an initial state, a transition, and an end state. This is sometimes called state mutation. Traditional imperative-style programs are often described as a series of mutations, separated with condition testing.

One major difference between imperative programming and FP is that in FP there are no side effects. This means, among other things,

- No mutation of variables

- No printing to the console or to any device

- No writing to files, databases, networks, or whatever

- No exception throwing

When I say “no side effects,” I mean no observable side effects. Functional programs are built by composing functions that take an argument and return a value, and that’s it. You don’t care about what’s happening inside the functions, because, in theory, nothing is happening ever. But in practice, programs are written for computers that aren’t functional at all.

Some code in the imperative style:


import java.util.*;
 
public class FindNemo {
  public static void main(String[] args) {
    List names = 
      Arrays.asList("Dory", "Gill", "Bruce", "Nemo", "Darla", "Marlin", "Jacques");
 
    findNemo(names);
  }                 
   
  public static void findNemo(List names) {
    boolean found = false;
    for(String name : names) {
      if(name.equals("Nemo")) {
        found = true;
        break;
      }
    }
     
    if(found)
      System.out.println("Found Nemo");
    else
      System.out.println("Sorry, Nemo not found");
  }
}

The same code in the declarative style:


public static void findNemo(List names) {
  if(names.contains("Nemo"))
    System.out.println("Found Nemo");
  else
    System.out.println("Sorry, Nemo not found");
}

While programming in a functional style is always declarative, simply using a declarative style doesn't equate to functional programming. This is because functional programming combines declarative methods with higher order functions.

import java.util.*;
 
public class UseMap {
  public static void main(String[] args) {
    Map<String, Integer> pageVisits = new HashMap<>();            
     
    String page = "https://agiledeveloper.com";
     
    incrementPageVisit(pageVisits, page);
    incrementPageVisit(pageVisits, page);
     
    System.out.println(pageVisits.get(page));
  }
   
  public static void incrementPageVisit(Map<String, Integer> pageVisits, String page) {
    if(!pageVisits.containsKey(page)) {
       pageVisits.put(page, 0);
    }
     
    pageVisits.put(page, pageVisits.get(page) + 1);
  }
}

the same in functional style:


public static void incrementPageVisit(Map<String, Integer> pageVisits, String page) {
    pageVisits.merge(page, 1, (oldValue, value) -> oldValue + value); 
}