Functional programming is a well-known concept in Javascript and Python. But, it’s a fancy thing for a java developer. Prior to Java 8, Java has been a pure OOP where objects are first-class citizens. We used to do imperative coding in Java. From version 8 onwards, Java brings in the new capabilities to do functional programming. However, Java is not a functional programming language like Javascript.

Image for post

What is functional programming?

In order to understand functional programming, we need to understand some basic concepts first.

  1. Imperative programming: In this programming paradigm, we need to tell what is required to be done and how can it be done? It relies on statements like if, when, for, forEach, etc.
public class Programming {

    //remove duplicates from the list of numbers
    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1,2,2,3,4,5,6,7,7,8,9,9,8);

        //imperative style of removing duplicates from the list
        List<Integer> newList = new ArrayList<>();

        for(Integer number: numbers) {
            if(!newList.contains(number)) {
                newList.add(number);
            }
        }
        System.out.println(newList);
    }
}

2. Declarative programming: It just requires what to do, it depends on expressions rather than statements. Declarative code focuses on building the logic of software without actually describing its flow.

#java #functional-programming #function

The new aspect of Java with functional programming
1.05 GEEK