avatarRaouf Makhlouf

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3054

Abstract

ava 8 is to use something called a <b><i>BiConsumer</i></b>. As its prefix implies, a <b><i>BiConsumer </i></b>takes two parameters, and it “consumes” them both, meaning that they are “swallowed” so that nothing is returned. Again, we can test this thesis very easily like this:</p> <figure id="34a1"> <div> <div>

            <iframe class="gist-iframe" src="/gist/Raouf25/a09b37b5f910a53ae4a0e207142ec4db.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="8b79">So, now you can also use your setters programmatically in your Java 8 code.</p><h2 id="4be0">A Functional View of Constructors</h2><p id="75ff">Constructing objects in Java is sometimes considered “magic”, but it is really nothing special about a constructor from a functional point of view. 

A constructor is something like a Function that takes anything from zero to many parameters and produces an Object.</p><h2 id="24d9">The Default Constructor</h2><p id="ddda">We had two constructors in our example, and now we will start by looking at the default constructor City(). The constructor apparently creates a City without using any parameter. In a Java 8 context, it is really a Supplier, and more specifically, since it supplies cities, it is a<b><i> Supplier<City></i></b>. Again, we can prove this statement by trying this simple code:</p> <figure id="c958"> <div> <div>

            <iframe class="gist-iframe" src="/gist/Raouf25/7bf46cb947922b5be3769e0dc3536349.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="d1b7">This is great! Now we can create different objects using simple constructor references. 

Very useful in dynamic programming code where you do not know the type of object you want to create until you run your code. Note how we obtain a method reference for the constructor using the <b><i>::new </i></b>reference.</p><h2 id="87f4">Parameter Constructors</h2><p id="90f2">Now it becomes a bit more tricky. A constructor that takes parameters is more than just a Supplier because it needs the input parameters before the object can be created. In our example, we have a constructor with two parameters, and thus we can use Java 8’s <b><i>BiFunction</i></b>. The <b><i>BiFunction </i></b>takes two parameters and can return something else, potentially depending on the two parameters. Because our City constructor takes two strings, we want to use a <b><i>BiFunction<String, String, City></i></b> which implies that we take two strings and maps them to a City. Again, we put together some code to show the concept:</p> <figure id="6a18"> <div> <div>

            <iframe class="gist-iframe" src="/gist/Raouf25/5af5292cfc93ddda3426623b2266d2bb.js"

Options

allowfullscreen="" frameborder="0" height="undefined" width="undefined"> </div> </div> </figure></iframe></div></div></figure><p id="ff80">But wait there!</p><p id="102e">We used the same method reference City::new for both the parameter-less default constructor and the two-parameter constructor, and yet Java 8 can distinguish between them!</p><p id="94da">How can City::new have two different meanings? The answer is that Java 8 can infer which constructor it shall select because it can match the different constructors against the expected result. Because we want a BiFunction, it understands that it shall return a method reference to the two-parameter constructor and not the default constructor with no parameters!</p><p id="54e5"><b><i>In nutshell</i></b></p> <figure id="4b66"> <div> <div>

            <iframe class="gist-iframe" src="/gist/Raouf25/027c93ae4d5a362538a8f42fce30e916.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="f46e">In this post, we have devised a way of obtaining a fully functional view of <i>POJO</i>s. By mastering these techniques, you will be able to shake new life into your old <i>POJO</i>s and your Java 8 code.</p><p id="8c7b">Good luck with your functions!</p><p id="96dc">Thank you for your time. I hope you got interesting stuff for you and enjoyed the reading.</p><p id="4139">✉ <a href="https://softwaretipsandtricks.substack.com/"><b><i>Let’s keep in touch! Sign up for my weekly newsletter</i></b></a></p><p id="42be"><b>❤ If you liked this post, you might also love:</b></p><div id="3c92" class="link-block">
      <a href="https://readmedium.com/java-8-how-to-handle-exceptions-in-a-stream-f884172b10">
        <div>
          <div>
            <h2>Java 8, How to handle exceptions in a stream?</h2>
            <div><h3>Moving to Java 8 adopt the new approach pretty easily, and the resulting code tends to be shorter and easier to follow…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*wLszx79V9y8Piln2)"></div>
          </div>
        </div>
      </a>
    </div><div id="2a70" class="link-block">
      <a href="https://readmedium.com/useful-git-alias-926f27a27f92">
        <div>
          <div>
            <h2>Useful git alias</h2>
            <div><h3>It’s well known That developers want to optimize and simplify repetitive tasks. We like to make shortcuts, type fewer…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*L7xOvjD6s_6AQHOW)"></div>
          </div>
        </div>
      </a>
    </div></article></body>

Java 8 functional programming, how I do?

Since Java 8 is released, we can have another view on the legacy Plain Ordinary Java Objects (or short “POJO”) and work with them in a completely new way. Happy Reading!

Consider the following simple class that models a City:

we can easily create cities like this:

This is all old school, but what is really a getter and a setter in a Java 8 functional context? How can you model different constructors in Java 8?

A Functional View of Getters

A getter is something that can take a POJO and convert it into something else. This corresponds to a Java 8 Function. Great!!… In our City example, the getName() getter corresponds to a Java 8 Function<City, String> because the getter takes a City and turns it into a String. In other words, it maps a City to a String. We can easily prove our postulate by the following code snippet:

The code for San Francisco is SF

So, now you can use a Function instead of calling the getter directly. This can be very useful when you want to use different getters programmatically in your code, for example, by providing the getter as a Function in methods you call. It is also handy because it allows you to use the methods programmatically without resorting to reflection in some cases.

A Functional View of Setters

The functional view on setters is slightly more complex than for getters. We really use two parameters: the POJO itself and some value we want to set in the POJO. As opposed to a getter, we do not want to return anything. The way to go in Java 8 is to use something called a BiConsumer. As its prefix implies, a BiConsumer takes two parameters, and it “consumes” them both, meaning that they are “swallowed” so that nothing is returned. Again, we can test this thesis very easily like this:

So, now you can also use your setters programmatically in your Java 8 code.

A Functional View of Constructors

Constructing objects in Java is sometimes considered “magic”, but it is really nothing special about a constructor from a functional point of view. A constructor is something like a Function that takes anything from zero to many parameters and produces an Object.

The Default Constructor

We had two constructors in our example, and now we will start by looking at the default constructor City(). The constructor apparently creates a City without using any parameter. In a Java 8 context, it is really a Supplier, and more specifically, since it supplies cities, it is a Supplier<City>. Again, we can prove this statement by trying this simple code:

This is great! Now we can create different objects using simple constructor references. Very useful in dynamic programming code where you do not know the type of object you want to create until you run your code. Note how we obtain a method reference for the constructor using the ::new reference.

Parameter Constructors

Now it becomes a bit more tricky. A constructor that takes parameters is more than just a Supplier because it needs the input parameters before the object can be created. In our example, we have a constructor with two parameters, and thus we can use Java 8’s BiFunction. The BiFunction takes two parameters and can return something else, potentially depending on the two parameters. Because our City constructor takes two strings, we want to use a BiFunction<String, String, City> which implies that we take two strings and maps them to a City. Again, we put together some code to show the concept:

But wait there!

We used the same method reference City::new for both the parameter-less default constructor and the two-parameter constructor, and yet Java 8 can distinguish between them!

How can City::new have two different meanings? The answer is that Java 8 can infer which constructor it shall select because it can match the different constructors against the expected result. Because we want a BiFunction, it understands that it shall return a method reference to the two-parameter constructor and not the default constructor with no parameters!

In nutshell

In this post, we have devised a way of obtaining a fully functional view of POJOs. By mastering these techniques, you will be able to shake new life into your old POJOs and your Java 8 code.

Good luck with your functions!

Thank you for your time. I hope you got interesting stuff for you and enjoyed the reading.

Let’s keep in touch! Sign up for my weekly newsletter

❤ If you liked this post, you might also love:

Java
Java8
Functional Programming
Lambda
Programming
Recommended from ReadMedium