Thursday, February 08, 2018

Java 8 : Functions

Functional programming in Java, a useful feature was introduced with Java 8 (java.util.function package).

With that, we have the ability to define a function, pass functions to a method as arguments or use already defined methods as functions.

I'll use a simple example to explain a function.


Function  myFunction = n -> n * n
 
I have defined a function here, named as "myFunction". It takes an argument of type Integer and returns an Integer (the square of the number passed in).

We can pass this function to other methods as an argument. The method below takes in a function and an integer, then calls function's apply method with the passed in integer. That applies function to the integer.

public int myMethod(Function func, Integer number) {
    return func.apply(number)
}
 
If we call above method with number 5 like below, we'll get 25 in return.
 
int answer = myMethod(myFunction, 5);

Also we are able to use already defined methods as functions. As an example, if we have following method implemented in myClass,


public class MyClass {
  public Integer calculateSquare(Integer number) {
    return number * number;
  }
}
 
then we can define a function like this that will act similar to myFunction.

Function func = MyClass::calculateSquare;
  
You can find all sorts of functions that you can define in this link. I only used one kind (Function : Represents a function that accepts one argument and produces a result) in the example.

28 comments:

Blogger said...

Did you know there is a 12 word sentence you can tell your man... that will induce deep emotions of love and impulsive attraction for you buried inside his heart?

Because deep inside these 12 words is a "secret signal" that triggers a man's impulse to love, idolize and look after you with all his heart...

=====> 12 Words Who Trigger A Man's Desire Impulse

This impulse is so built-in to a man's genetics that it will drive him to try harder than ever before to build your relationship stronger.

As a matter of fact, triggering this powerful impulse is absolutely important to achieving the best possible relationship with your man that the second you send your man a "Secret Signal"...

...You'll immediately find him open his soul and mind for you in such a way he haven't expressed before and he'll perceive you as the one and only woman in the galaxy who has ever truly tempted him.

Anonymous said...

The blog is very useful and informative thanks for sharing Ethical Hacking Training

Elegant IT Services said...

Nice Post...Thanks for sharing the Blog...
Best IT and Non-IT Course Training Institute in Bangalore

i Digital Academy said...

Very nice information. I would like to appreciate you. Keep it up...
Digital Marketing in Bangalore

Sonu said...

thanks for valuable information.

fivetech said...

OPPO Find X3 Series
Find X2 Pro 5G
Find X2 5G

Valentin said...

Korzystne bonusy, licencjonowane oprogramowanie, uczciwe wypłaty, prawdziwe wygrane top10casinoexpert.pl

Digital Blogger said...

The website loading speed is amazing. It sort of feels that you’re doing any distinctive trick. Moreover, The contents are masterwork. you have performed a excellent activity in this matter!
Also Read - bootstrap navbar close on click, input type file css

Hdhdhdhr said...

Ajkeritnews24.xyz | Tech News Bangla - বাংলা ভাষার নিয়মিত একটি টেক ব্লগ |

Ajker IT News 24

Hdhdhdhr said...

Good Health Good Mind Good Life goodhealth247.

GOODHEALTH247

jenny said...


Really informative and inoperative, Thanks for the post and effort! Please keep sharing more such blog if want
more information about topic Visit Us our Website at movies flame .

Bharathi said...

Hi.
The website's loading speed is truly impressive, giving the impression that there might be some unique optimizations at play. Furthermore, the content itself is of exceptional quality, demonstrating a masterful work. Here is sharing some Oracle CPQ BML Training information may be its helpful to you.

Oracle CPQ BML Training

seodigiperform said...

Great breakdown of Java 8 functions! The use of Function, Consumer, and Predicate really streamlines coding, especially when dealing with complex logic. As someone who is into both tech and marketing, I can see the parallels in simplifying workflows in digital marketing as well. Speaking of efficiency, courses like the Digital Marketing Course In Delhi also emphasize automation and optimization, which is crucial for marketing professionals today. Thanks for sharing this informative post!

Chaitali sarkar said...

Awesome Blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog...
digital marketing courses in delhi

Arun00111 said...

This article provides a great introduction to functional programming in Java 8 using the java.util.function package. The explanation of how to define a function, pass it as an argument, and reference existing methods is clear and easy to follow. The example showcasing Function for calculating the square of a number perfectly illustrates how lambda expressions and method references simplify code.

Additionally, for developers looking to leverage Java for data-intensive applications, exploring data Science courses in Delhi can be a smart move. Combining Java’s functional programming capabilities with data science techniques opens up opportunities in big data processing, machine learning pipelines, and more.

Overall, this guide offers a solid foundation for understanding Java 8’s functional features—looking forward to more examples using other functional interfaces like Predicate, Supplier, and Consumer!

Arun00111 said...

This explanation of Java 8 Functions and their role in functional programming is spot on! The introduction of the java.util.function package has indeed made Java more expressive and powerful, especially with features like passing functions as arguments and using method references (MyClass::calculateSquare).

For those looking to strengthen their Java skills further, especially in areas like functional programming, it's beneficial to complement this knowledge with data Science courses in Delhi . These courses often cover Java's applications in data processing, machine learning, and big data technologies where functional programming concepts shine, especially in stream processing and parallel computations.

The clear examples provided here, such as defining myFunction and using it in myMethod, make the concept easy to grasp. Exploring other functional interfaces like Predicate, Supplier, and Consumer will also add depth to one’s understanding.

maziniimskills said...

thanks for sharing such a wonderful posts
Medical Coding Courses in Kochi

maziniimskills said...

it was a nice posts
Medical Coding Courses in Kochi

Ramm00111 said...

Great introduction to functional programming in Java 8! 🚀 The ability to use functions as first-class citizens makes Java much more expressive and flexible, especially for cleaner, more modular code. The explanation of Function and method references (MyClass::calculateSquare) is spot on—this is a game-changer for writing concise and reusable logic.

For those looking to explore opportunities beyond tech, Medical Coding Courses in Delhi can be a great option. The field of healthcare data management is growing rapidly, and coding skills can be a valuable asset there too!

Looking forward to more Java 8 functional programming insights! 🎯

Judith said...

The Function Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java.
Medical Coding Course in Hyderabad

Fardheen Musthafa said...

Wonderful Content with such a great valuable information. Thanks for sharing this great post. Please Visit: Medical Coding Courses in Chennai

Fardheen Musthafa said...

Wonderful Content with such a great valuable information. Thanks for sharing this great post. Please Visit: Medical Coding Courses in Chennai

jud said...

The Function Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java.
Medical Coding Course in Hyderabad

maziniimskills said...

good blog
Medical Coding Course in Hyderabad

maziniimskills said...

fantastic blog
Medical Coding Course in Hyderabad

Data Analytics With Power BI said...

Great introduction to Java 8 functional programming! 🚀

Java 8 functions allow us to treat methods as first-class citizens, making code more modular and reusable.

The Function interface is especially useful for lambda expressions and method references.

Using MyClass::calculateSquare as a method reference demonstrates how Java 8 simplifies functional programming.

For those diving into Java and functional programming, mastering lambda expressions and function interfaces is a game-changer. Similarly, if you're exploring Medical Coding Courses in Delhi, structured programming skills can help with healthcare data processing and automation. 🏥💻

Thanks for sharing this! Java 8 functional programming is a powerful tool for modern development. 🔥

AI Readers club said...

Really informative post on Java 8 functions! The breakdown of functional interfaces and examples made it much easier to understand lambda expressions.

Medical Coding Courses in Delhi

Shubh said...

Medical Coding Course in Hyderabad Very amazing blog

Related Posts with Thumbnails