<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Italo Costa on Medium]]></title>
        <description><![CDATA[Stories by Italo Costa on Medium]]></description>
        <link>https://medium.com/@italorcosta?source=rss-59a2730f0b52------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0JzL6D2-CkBRwl_N1PR1mA.jpeg</url>
            <title>Stories by Italo Costa on Medium</title>
            <link>https://medium.com/@italorcosta?source=rss-59a2730f0b52------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 22:52:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@italorcosta/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Difference between ‘compose’ and ‘andThen’ Function interface methods in Java]]></title>
            <link>https://italorcosta.medium.com/difference-between-compose-and-andthen-methods-on-java-function-interface-20c8b8a62e78?source=rss-59a2730f0b52------2</link>
            <guid isPermaLink="false">https://medium.com/p/20c8b8a62e78</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[decorator-pattern]]></category>
            <dc:creator><![CDATA[Italo Costa]]></dc:creator>
            <pubDate>Fri, 09 Apr 2021 14:40:10 GMT</pubDate>
            <atom:updated>2023-07-08T15:08:22.644Z</atom:updated>
            <content:encoded><![CDATA[<h3>Difference between ‘compose’ and ‘andThen’ methods on Java Function interface</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*g2oeDAd6UwjcnxJ2" /><figcaption>Photo by <a href="https://unsplash.com/@cgower?utm_source=medium&amp;utm_medium=referral">Christopher Gower</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>One of the most important features added to Java 8 was the Function interface. The developers can implement this interface by adding any functionality through the functional paradigm and execute it whenever necessary with few lines of code.</p><h4><strong>Using Function interface</strong></h4><p>Below is a simple example on how to use the Function interface in Java:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cad698832297943f0bd53e631e8799f6/href">https://medium.com/media/cad698832297943f0bd53e631e8799f6/href</a></iframe><p>As you can see, the variable stringToUpperCase is an object of a Function interface implementation and it was created with the aim to transform any String in uppercase letters. Subsequently, that variable was used in two different situations, in the first one, the default method apply was used to execute that functionality in the personName variable, and in the last one, that variable was used as a parameter to the map method in the stream of animals list.</p><p>Result:</p><pre>PETER<br>DOG<br>CAT<br>LION<br>DOLPHIN<br>SHARK</pre><h4>Default methods in the Function interface</h4><p>The Function interface has four methods, two of which are used to create execution’s flows, concatenating functionalities through of the Decorator Design Pattern. They are compose<em> </em>and andThen<em> </em>methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed219764ede5b6708ee75a34ac9ee278/href">https://medium.com/media/ed219764ede5b6708ee75a34ac9ee278/href</a></iframe><p>Those two methods has almost same functionality, but there’s a small but relevant difference between them. Let´s go to another example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4cc374215da7196073ac5051deb0aeb/href">https://medium.com/media/c4cc374215da7196073ac5051deb0aeb/href</a></iframe><p>In the above example the values of the input or output parameters do not matter, so we’ll focus on the execution flow. Basically, the object f1 was created with the purpose of printing the word main on the console, but I’ve decided to add other functionalities by using Decorator Pattern, for that I’ve used the compose<em> </em>and andThen<em> </em>methods. Lastly, the apply<em> </em>method was invoked to execute all the accumulated functionalities.</p><p>Result:</p><pre>compose 4<br>compose 3<br>compose 2<br>compose 1<br>main<br>andThen 1<br>andThen 2<br>andThen 3<br>andThen 4</pre><p>As you can see, the execution order was not the same as that of the code declaration. That has happened because those two methods added its logic to different positions in the execution flow. The picture below explains it better:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iLFvH8z0jYckTvu3FwuUsg.jpeg" /><figcaption>Execution flow usingDecorator Pattern in the Function interface</figcaption></figure><p>Therefore, when we need to use Decorator Pattern in Function interface to accumulate functionalities, we can use those two methods. However, we need to be sure which execution order we need.</p><ul><li><strong>compose</strong>: The functionality will be added to be executed in the first position of the execution flow. (LIFO — Last in-First out)</li><li><strong>andThen</strong>: The functionality will be added to be executed in the last position of the execution flow. (FIFO — First in-First out)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20c8b8a62e78" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>