Java8 - Functional programming concepts in Java

Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 04:30:59 Viewed : 294

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. While Java is primarily an object-oriented language, Java 8 and subsequent versions introduced features that bring functional programming concepts to the language. Here are some key functional programming concepts in Java:

  1. First-Class Functions:

    • In Java: Java 8 introduced lambda expressions, which allow functions to be treated as first-class citizens. This means you can pass functions as arguments to other functions, return functions from other functions, and assign functions to variables.
    // Example of passing a lambda expression as an argument List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); names.forEach(name -> System.out.println("Hello, " + name));
  2. Higher-Order Functions:

    • In Java: Higher-order functions take one or more functions as arguments or return a function. Java is support for lambda expressions enables the creation of higher-order functions.
    // Example of a higher-order function Function<Integer, Integer> multiplyByTwo = x -> x * 2; Function<Integer, Integer> addOne = x -> x + 1; // Combining functions Function<Integer, Integer> combined = multiplyByTwo.andThen(addOne); int result = combined.apply(3); // Result: (3 * 2) + 1 = 7
  3. Immutability:

    • In Java: While Java itself is not a purely functional language, it supports immutability through the use of the final keyword and the creation of immutable classes. Immutable objects cannot be modified after they are created.
    // Example of an immutable class public final class ImmutablePerson { private final String name; private final int age; public ImmutablePerson(String name, int age) { = name; this.age = age; } // Getter methods... }
  4. Pure Functions:

    • In Java: A pure function is a function whose output is solely determined by its input, without observable side effects. While Java methods can have side effects, functional programming encourages writing pure functions whenever possible.
    // Example of a pure function public int add(int a, int b) { return a + b; }
  5. Referential Transparency:

    • In Java: Referential transparency means that a functions result depends only on its input, and calling the function with the same input always produces the same output. Pure functions and immutability contribute to referential transparency.
    // Referential transparency example int result1 = add(2, 3); // Result: 5 int result2 = add(2, 3); // Result: 5 // Since add is a pure function, result1 and result2 are guaranteed to be the same.
  6. Lazy Evaluation:

    • In Java: While Java is not purely lazy, it supports a form of lazy evaluation through the Stream API. Operations on streams are evaluated only when a terminal operation is invoked.
    // Lazy evaluation with streams List<String> words = Arrays.asList("apple", "banana", "orange"); List<String> filteredWords = .filter(s -> { System.out.println("Filtering: " + s); return s.length() > 5; }) .limit(2) .collect(Collectors.toList());

These functional programming concepts in Java provide developers with tools to write more expressive, modular, and maintainable code. While Java is not a purely functional language, these features allow developers to incorporate functional programming principles into their applications.

Related Articles

Leave a Comment: