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.
In order to understand functional programming, we need to understand some basic concepts first.
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