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.

7 comments:

Susmitha Bommepalli said...

Very nice information. I would like to appreciate you. Keep it up!
Best Data Science Online Training Institute In Hyderabad | Online Data Science Training
Data Science Online Training Institute In Hyderabad
Data science online training in hyderabad
Best data science training in hyderabad


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

Kuma Herbals said...

Great post!! Thanks for sharing...
Unani Treatment in Bangalore

Sonu said...

thanks for valuable information.

Related Posts with Thumbnails