Java functional programming example
("** Student marks after adding internal marks **") StudentList.add(new Student(4, 20, "Dom")) įunction markIncrement = mark -> mark + 20 StudentList.add(new Student(3, 80, "Clair")) StudentList.add(new Student(2, 65, "Bob")) StudentList.add(new Student(1, 45, "Alice")) StudentPredicate.java package com.javainterviewpoint Public Student(int id, int mark, String name) Student.java package com.javainterviewpoint Let’s create a function that adds an internal mark of 20 to each student. The Function can be used on custom objects as well. NumberList.stream().map(num -> (squareFunction.apply(num))) package com.javainterviewpoint įunction squareFunction = num -> num * num Let’s create a Function which squares the number passed to it. The Function interface can also be used in the Streams the map() method of the stream takes the Function as its argument. ("Length of \"Welcome\" is: " + length.apply("Welcome")) ("Length of \"Hello\" is: " + length.apply("Hello")) Let’s create a new Function, which takes in a string input and returns the length of the string, which will be an integer output. There are no rules that the return type has to be the same as the input type. We have passed a string as input and got a string as output. We can invoke the capitalize function by passing a string argument to the apply() method. In the above code, we have created a Function that capitalizes the string passed to it. ( capitalize.apply("JaVaInTeRvIeWpOiNt")) package com.javainterviewpoint įunction capitalize = val -> val.toUpperCase() Let’s build a Function that returns the capitalizes of the String passed to it. The apply() method of the Function interface can take any object type as an argument and can return any object type. static Function identity() – This static method returns its input argument.The before function will be executed first and on the result input function will be executed. default Function compose(Function before) – This is a default method, returns a composed function.The input function will be executed and on the result after function will be executed. default Function andThen(Function after) – This is a default method, returns a composed function.R apply(T t) – This method takes a single generic argument T and returns an object of type R.The Function interface is similar to Predicate functional interface the only change is the return type. Structured programming can’t handle complex problems, whereas Functional programming handles any level of a complex problem.Whenever we create a Lambda Expression, which takes a single input and can return any value, then the Function can be used as a target for the lambda expression. Structured programming less secure, whereas Functional programming highly secure. Structured programming divided small units or functions, whereas Functional programming divided into small run-time entities called objects. Structured programming follows a top-down approach, whereas Functional programming follows bottom to top. Structured programming gives importance to logical structure or process, whereas Functional programming mainly focuses on data. How does Functional programming work in Java?īefore we get into the functional programming concept, let’s see the difference between Functional and Structure programming. Method Reference functional Programming.Lambda Expressions functional Programming.
JAVA FUNCTIONAL PROGRAMMING EXAMPLE SOFTWARE
Web development, programming languages, Software testing & others
JAVA FUNCTIONAL PROGRAMMING EXAMPLE FREE
Start Your Free Software Development Course