<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://rafaelnaufal.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://rafaelnaufal.com/" rel="alternate" type="text/html" /><updated>2025-08-30T22:47:13+00:00</updated><id>https://rafaelnaufal.com/feed.xml</id><title type="html">Rafael Naufal’s blog</title><subtitle>Communicating my thoughts on software development</subtitle><author><name>Rafael Naufal</name></author><entry><title type="html">About patterns for handling errors in Kafka applications</title><link href="https://rafaelnaufal.com/2024/11/23/about-patterns-for-handling-errors-in-kafka-applications/" rel="alternate" type="text/html" title="About patterns for handling errors in Kafka applications" /><published>2024-11-23T00:00:00+00:00</published><updated>2024-11-23T00:00:00+00:00</updated><id>https://rafaelnaufal.com/2024/11/23/about-patterns-for-handling-errors-in-kafka-applications</id><content type="html" xml:base="https://rafaelnaufal.com/2024/11/23/about-patterns-for-handling-errors-in-kafka-applications/"><![CDATA[<p>I have just read an interesting article about <a href="https://www.confluent.io/blog/error-handling-patterns-in-kafka/">patterns for handling errors in Kafka</a>. The article presents four patterns for handling errors in Kafka applications:</p>

<ul>
  <li>Stop on error</li>
  <li>Dead Letter Queue</li>
  <li>Retry topic and retry application</li>
  <li>Maintain order of redirected events</li>
</ul>

<p>I have already used the <a href="https://spring.io/projects/spring-kafka">spring-kafka</a> <a href="https://docs.spring.io/spring-kafka/reference/retrytopic/retry-config.html#using-the-retryabletopic-annotation">@RetryableTopic</a> annotation where it handles the creation of the retry and dead letter topics automatically for Java and Kotlin backend applications. It is very useful and saves a lot of time in these scenarios.</p>

<p>How do you handle errors in Kafka applications? Drop you comments below about what you do in these situations.</p>]]></content><author><name>Rafael Naufal</name></author><category term="development" /><category term="java" /><category term="Kafka" /><category term="kotlin" /><category term="programming" /><category term="spring" /><category term="spring boot" /><category term="development" /><category term="java" /><category term="Kafka" /><category term="kotlin" /><category term="programming" /><category term="spring" /><category term="spring boot" /><summary type="html"><![CDATA[I have just read an interesting article about patterns for handling errors in Kafka. The article presents four patterns for handling errors in Kafka applications:]]></summary></entry><entry><title type="html">About using Test Doubles in tests</title><link href="https://rafaelnaufal.com/2024/03/08/about-using-test-doubles-in-tests/" rel="alternate" type="text/html" title="About using Test Doubles in tests" /><published>2024-03-08T00:00:00+00:00</published><updated>2024-03-08T00:00:00+00:00</updated><id>https://rafaelnaufal.com/2024/03/08/about-using-test-doubles-in-tests</id><content type="html" xml:base="https://rafaelnaufal.com/2024/03/08/about-using-test-doubles-in-tests/"><![CDATA[<p>I have just read an interesting chapter from the <a href="https://abseil.io/resources/swe-book/html/toc.html">Software Engineering book at Google</a> about using <a href="https://abseil.io/resources/swe-book/html/ch13.html">Test Doubles</a> in tests.</p>

<p>The chapter discuss the various techniques of using test doubles in tests along with the pros and cons of using real implementations, fakes, stubbing with the support of mocking frameworks and interaction testing. It also refers to the benefits of running <a href="https://martinfowler.com/bliki/ContractTest.html">Contract Tests</a> against the API’s public interface to catch contract changes prior to the production environment.</p>

<p>In my opinion, it is worth reading it to be aware of good practices when writing unit tests. Drop your comments here about what you think about it.</p>]]></content><author><name>Rafael Naufal</name></author><category term="java" /><category term="junit" /><category term="mockito" /><category term="programming" /><category term="test" /><category term="testing" /><category term="tests" /><category term="unittests" /><category term="fakes" /><category term="java" /><category term="junit" /><category term="mocking" /><category term="mockito" /><category term="programming" /><category term="stubbing" /><category term="testdoubles" /><category term="testing" /><category term="tests" /><category term="unittests" /><summary type="html"><![CDATA[I have just read an interesting chapter from the Software Engineering book at Google about using Test Doubles in tests.]]></summary></entry><entry><title type="html">About JUnit 5 parameterized tests</title><link href="https://rafaelnaufal.com/2022/12/18/about-junit-5-parameterized-tests/" rel="alternate" type="text/html" title="About JUnit 5 parameterized tests" /><published>2022-12-18T00:00:00+00:00</published><updated>2022-12-18T00:00:00+00:00</updated><id>https://rafaelnaufal.com/2022/12/18/about-junit-5-parameterized-tests</id><content type="html" xml:base="https://rafaelnaufal.com/2022/12/18/about-junit-5-parameterized-tests/"><![CDATA[<p>Interesting <a href="https://medium.com/javarevisited/unleash-the-true-potential-of-junit5-and-b-d-d-f4ae4ff90048">article</a> about <a href="https://junit.org/junit5/docs/current/user-guide/">JUnit 5</a> parameterized tests using BDD (Behaviour-Driven Development) style. It shows an example of using <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-sources-CsvSource">@CsvSource</a> as arguments source and a specific implementation of an argument <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-argument-conversion-explicit">converter</a>. Have you ever used parameterized tests in your unit tests?</p>]]></content><author><name>Rafael Naufal</name></author><category term="java" /><category term="junit5" /><category term="kotlin" /><category term="programming" /><category term="testing" /><category term="java" /><category term="junit5" /><category term="kotlin" /><category term="programming" /><category term="tests" /><summary type="html"><![CDATA[Interesting article about JUnit 5 parameterized tests using BDD (Behaviour-Driven Development) style. It shows an example of using @CsvSource as arguments source and a specific implementation of an argument converter. Have you ever used parameterized tests in your unit tests?]]></summary></entry><entry><title type="html">Writing your own custom Java stream collector</title><link href="https://rafaelnaufal.com/2020/03/29/writing-your-own-custom-java-stream-collector/" rel="alternate" type="text/html" title="Writing your own custom Java stream collector" /><published>2020-03-29T00:00:00+00:00</published><updated>2020-03-29T00:00:00+00:00</updated><id>https://rafaelnaufal.com/2020/03/29/writing-your-own-custom-java-stream-collector</id><content type="html" xml:base="https://rafaelnaufal.com/2020/03/29/writing-your-own-custom-java-stream-collector/"><![CDATA[<p>One of the common operations of the <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/stream/Collectors.html">Collectors</a> API introduced in <a href="http://www.oracle.com/technetwork/pt/java/javase/downloads/jdk8-downloads-2133151.html">Java 8</a> is the possibility to collect results into a result container like <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/List.html">List</a>, <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Set.html">Set</a> or <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/Map.html">Map</a>. The following example uses the <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/stream/IntStream.html#collect(java.util.function.Supplier,java.util.function.ObjIntConsumer,java.util.function.BiConsumer)">collect()</a> method to generate a <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/HashSet.html">HashSet</a> containing unique numbers:</p>

<script src="https://gist.github.com/rnaufal/98eb7b5c29f7bae46612158ea3b1c934.js"></script>
<p>Now suppose a given string and the need to compute some summaries on it, like the number of uppercase, lowercase, invalid chars and how many digits are present on that string.</p>

<p>Applying a reduce operation on each needed summary operation would result in more than one pass through the data, like this (here I am using the <a href="https://developer.oracle.com/java/jdk-10-local-variable-type-inference.html">var</a> keyword introduced in <a href="https://www.oracle.com/java/technologies/java-archive-javase10-downloads.html">Java 10</a>):</p>

<script src="https://gist.github.com/rnaufal/ea6d58f6a609305e3835f2d9cf2fade7.js"></script>
<p>In the above code excerpt we are iterating three times through the string to compute the summaries. If we want to iterate just one time on the give string and get the desired results, we could fallback to the traditional imperative approach:</p>

<script src="https://gist.github.com/rnaufal/69202d997e61dea7fead4d23a1ad299c.js"></script>
<p>Despite the solution above, there is another one: writing your own custom stream collector. This custom collector can compute the number of uppercase, lowercase, invalid chars and how many digits are present on the given string, in a single pass through the data. It is possible to make it run in <code class="language-plaintext highlighter-rouge">parallel</code> with the <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/stream/Stream.html">Streams</a> API as well.</p>

<p>The custom stream collector shown here uses the <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html#chars()">chars</a> method of the <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/String.html">String</a> class which returns an <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/stream/IntStream.html">IntStream</a>. The <code class="language-plaintext highlighter-rouge">IntStream</code> class contains a <code class="language-plaintext highlighter-rouge">collect()</code> method that computes a mutable reduction on the elements and returns its result in a container class.</p>

<p>The next example shows the container class code. It receives and accumulates each <code class="language-plaintext highlighter-rouge">char</code> of the String in the <code class="language-plaintext highlighter-rouge">accept()</code> method, thus categorizing it as a digit, uppercase char, lowercase char or as an invalid char.</p>

<script src="https://gist.github.com/rnaufal/cfaadb809840c2408297a9c44e948287.js"></script>
<p>The complete implementation of the <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/stream/IntStream.html#collect(java.util.function.Supplier,java.util.function.ObjIntConsumer,java.util.function.BiConsumer)">collect()</a> method which produces the result of the reduction in the <a href="https://github.com/rnaufal/blog/blob/master/summaries/src/main/java/br/com/rnaufal/summaries/CharSummaryStatistics.java">CharSummaryStatistics</a> class is shown below. As a new char arrives, it is categorized in the <code class="language-plaintext highlighter-rouge">CharSummaryStatistics.accept()</code> method. The <code class="language-plaintext highlighter-rouge">CharSummaryStatistics.combine()</code> method is used to merge partial results.</p>

<script src="https://gist.github.com/rnaufal/008e3fe2b338006c3ca2bce0025f76f7.js"></script>
<p>Have you ever written a custom Java stream collector? Please drop your comments here.</p>

<p>The complete source code can be found on <a href="https://github.com/rnaufal/blog/tree/master/summaries">GitHub</a>.</p>

<p>As a reference for writing this post, this <a href="https://developer.ibm.com/articles/j-java-streams-2-brian-goetz/">article</a> is part of a series about <a href="https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/stream/Stream.html">Streams</a> and it is a good reference for learning how to aggregate and collect with Streams. All the articles from this series about Streams can be found <a href="https://developer.ibm.com/series/java-streams/">here</a>.</p>]]></content><author><name>Rafael Naufal</name></author><category term="collections" /><category term="collectors" /><category term="development" /><category term="functional" /><category term="java" /><category term="jdk14" /><category term="lambdas" /><category term="programming" /><category term="streams" /><category term="collections" /><category term="collectors" /><category term="development" /><category term="functional" /><category term="java" /><category term="jdk14" /><category term="lambdas" /><category term="programming" /><category term="streams" /><summary type="html"><![CDATA[One of the common operations of the Collectors API introduced in Java 8 is the possibility to collect results into a result container like List, Set or Map. The following example uses the collect() method to generate a HashSet containing unique numbers:]]></summary></entry><entry><title type="html">Jcombiner: Combinations of collections for Java</title><link href="https://rafaelnaufal.com/2018/11/27/jcombiner-combinations-of-collections-for-java/" rel="alternate" type="text/html" title="Jcombiner: Combinations of collections for Java" /><published>2018-11-27T00:00:00+00:00</published><updated>2018-11-27T00:00:00+00:00</updated><id>https://rafaelnaufal.com/2018/11/27/jcombiner-combinations-of-collections-for-java</id><content type="html" xml:base="https://rafaelnaufal.com/2018/11/27/jcombiner-combinations-of-collections-for-java/"><![CDATA[<p><a href="https://github.com/rnaufal/jcombiner">JCombiner</a> is a framework to generate combinations of collections for Java. I have written it in <a href="https://openjdk.java.net/projects/jdk/11/">Java 11</a> using Java 9 modules (<a href="https://openjdk.java.net/projects/jigsaw/quick-start">JPMS</a>) and <a href="https://gradle.org/">Gradle</a> as build tool. <a href="https://junit.org/junit5/">JUnit 5</a> and <a href="https://site.mockito.org/">Mockito</a> are used for unit testing and <a href="https://www.eclemma.org/jacoco/">Jacoco</a> for code coverage. <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/Stream.html">Streams</a> and the <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/Collectors.html">Collectors</a> API are extensively used throughout the development of JCombiner project.</p>

<p>Jcombiner’s source code is available under <a href="https://github.com/rnaufal/jcombiner">GitHub</a>.</p>

<p>Code examples of its usage can be found on GitHub <a href="https://github.com/rnaufal/jcombiner/blob/master/README.md#usage">here</a>. More examples can be found on <a href="https://github.com/rnaufal/jcombiner/tree/master/client">this</a> module inside JCombiner.</p>

<p>Share your comments about this framework here! Please feel free to <a href="https://help.github.com/articles/creating-a-pull-request">contribute</a> to it, more features are welcome!</p>]]></content><author><name>Rafael Naufal</name></author><category term="code" /><category term="collections" /><category term="collectors" /><category term="combinations" /><category term="development" /><category term="functional" /><category term="gradle" /><category term="java" /><category term="java11" /><category term="jcombiner" /><category term="jdk11" /><category term="junit" /><category term="junit5" /><category term="lambdas" /><category term="mockito" /><category term="programming" /><category term="streams" /><category term="code" /><category term="collections" /><category term="collectors" /><category term="combinations" /><category term="development" /><category term="functional" /><category term="gradle" /><category term="java" /><category term="java11" /><category term="jcombiner" /><category term="jdk11" /><category term="junit" /><category term="junit5" /><category term="lambdas" /><category term="mockito" /><category term="programming" /><category term="streams" /><summary type="html"><![CDATA[JCombiner is a framework to generate combinations of collections for Java. I have written it in Java 11 using Java 9 modules (JPMS) and Gradle as build tool. JUnit 5 and Mockito are used for unit testing and Jacoco for code coverage. Streams and the Collectors API are extensively used throughout the development of JCombiner project.]]></summary></entry><entry><title type="html">Refactoring large conditional method using method references</title><link href="https://rafaelnaufal.com/2018/09/10/refactoring-large-conditional-method-using-method-references/" rel="alternate" type="text/html" title="Refactoring large conditional method using method references" /><published>2018-09-10T00:00:00+00:00</published><updated>2018-09-10T00:00:00+00:00</updated><id>https://rafaelnaufal.com/2018/09/10/refactoring-large-conditional-method-using-method-references</id><content type="html" xml:base="https://rafaelnaufal.com/2018/09/10/refactoring-large-conditional-method-using-method-references/"><![CDATA[<p>Some years ago I wrote <a href="https://github.com/rnaufal/junit-parameters/">junit-parameters</a>, which is basically a custom JUnit test runner that make it possible to add parameters to <a href="https://junit.org/junit4">JUnit 4</a> test methods.</p>

<p>Browsing its source code <a href="https://www.sonarlint.org/intellij/">SonarLint</a> pointed me a large conditional <code class="language-plaintext highlighter-rouge">if/else</code> method from the <code class="language-plaintext highlighter-rouge">ParameterTypeConverterFactory</code> class:</p>

<script src="https://gist.github.com/rnaufal/f6ce25027520bdead5cee88f9db4382f.js"></script>

<p>This method converts the method parameter to its specific type based on its <code class="language-plaintext highlighter-rouge">Class</code> object. As it is few lines long, it showed me a good opportunity to refactor this code a little bit with a more elegant solution. This project has unit tests, so I could start refactoring it in <a href="http://wiki.c2.com/?RefactoringInVerySmallSteps">small steps</a> and start over again whether I have made a mistake.</p>

<p>I started by defining a <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html">functional interface</a> called <code class="language-plaintext highlighter-rouge">ParameterConverter</code>:</p>

<script src="https://gist.github.com/rnaufal/230a60294320d8f906cf9a7d99201fe1.js"></script>

<p>and I created an <a href="https://google.github.io/guava/releases/23.0/api/docs/com/google/common/collect/ImmutableMap.html">immutable map</a> which maps each <code class="language-plaintext highlighter-rouge">Class</code> object to its associated <code class="language-plaintext highlighter-rouge">ParameterConverter</code> instance (making use of <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html">method references</a>):</p>

<script src="https://gist.github.com/rnaufal/1fcb73dcc5c731847571bae35ff0bf56.js"></script>

<p>Then I refactored the original conditional method to get the <code class="language-plaintext highlighter-rouge">ParameterConverter</code> instance from the <code class="language-plaintext highlighter-rouge">convertersByClass</code> map and mapping to an <code class="language-plaintext highlighter-rouge">Optional</code> instance in case it didn’t exist.</p>

<p>After those refactorings, <a href="https://www.sonarlint.org/intellij/">SonarLint</a> stopped warning me. Below is the modified version of the original method with some helper methods:</p>

<script src="https://gist.github.com/rnaufal/2838c035c9cf4d36bd800a89d4024ec0.js"></script>

<p>The complete code can be found at my GitHub <a href="https://github.com/rnaufal/junit-parameters/blob/master/src/main/java/br/com/rnaufal/junit/parameters/statement/ParameterTypeConverterFactory.java">here</a>.</p>

<p>This was the first change of breaking this complicated conditional method into a more readable one. It was safe to do so because after each change I could run my unit tests suite to assert I haven’t broken anything. The next refactoring could be towards a more object-oriented approach like <a href="https://refactoring.com/catalog/replaceConditionalWithPolymorphism.html">Replace Conditional with Polymorphism</a>.</p>

<p>What did you think about this refactoring? Have you ever had such a situation? Drop your comments here!</p>]]></content><author><name>Rafael Naufal</name></author><category term="code" /><category term="development" /><category term="functional" /><category term="interface" /><category term="java" /><category term="java8" /><category term="jdk8" /><category term="lambdas" /><category term="optional" /><category term="polymorphism" /><category term="programming" /><category term="streams" /><category term="code" /><category term="development" /><category term="functional" /><category term="interface" /><category term="java" /><category term="java8" /><category term="jdk8" /><category term="lambdas" /><category term="optional" /><category term="polymorphism" /><category term="programming" /><category term="streams" /><summary type="html"><![CDATA[Some years ago I wrote junit-parameters, which is basically a custom JUnit test runner that make it possible to add parameters to JUnit 4 test methods.]]></summary></entry><entry><title type="html">Streams in JDK 8: The Good, the Bad, and the Ugly</title><link href="https://rafaelnaufal.com/2018/02/20/streams-in-jdk-8-the-good-the-bad-and-the-ugly/" rel="alternate" type="text/html" title="Streams in JDK 8: The Good, the Bad, and the Ugly" /><published>2018-02-20T00:00:00+00:00</published><updated>2018-02-20T00:00:00+00:00</updated><id>https://rafaelnaufal.com/2018/02/20/streams-in-jdk-8-the-good-the-bad-and-the-ugly</id><content type="html" xml:base="https://rafaelnaufal.com/2018/02/20/streams-in-jdk-8-the-good-the-bad-and-the-ugly/"><![CDATA[<p>Great <a href="https://www.youtube.com/watch?v=3CSfYGsmGEk">session</a> in <a href="https://www.oracle.com/javaone/index.html">JavaOne 2017</a> about <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html">Streams</a> and lambdas introduced in <a href="https://docs.oracle.com/javase/8/docs/api/">JDK8</a>.</p>

<p>The session shows many examples of Java code using <code class="language-plaintext highlighter-rouge">forEach()</code> with side effects and how to refactor them to a functional approach using streams and the <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html">Collectors</a> API.</p>

<p>What are your experiences using Streams and lambdas in JDK 8? Are you correctly using the Collectors API?</p>]]></content><author><name>Rafael Naufal</name></author><category term="collectors" /><category term="development" /><category term="functional" /><category term="java" /><category term="java8" /><category term="jdk8" /><category term="lambdas" /><category term="programming" /><category term="streams" /><category term="collectors" /><category term="development" /><category term="functional" /><category term="java" /><category term="java8" /><category term="jdk8" /><category term="lambdas" /><category term="programming" /><category term="streams" /><summary type="html"><![CDATA[Great session in JavaOne 2017 about Streams and lambdas introduced in JDK8.]]></summary></entry><entry><title type="html">3 Things Every Java Developer Should Stop Doing</title><link href="https://rafaelnaufal.com/2018/02/19/3-things-every-java-developer-should-stop-doing/" rel="alternate" type="text/html" title="3 Things Every Java Developer Should Stop Doing" /><published>2018-02-19T00:00:00+00:00</published><updated>2018-02-19T00:00:00+00:00</updated><id>https://rafaelnaufal.com/2018/02/19/3-things-every-java-developer-should-stop-doing</id><content type="html" xml:base="https://rafaelnaufal.com/2018/02/19/3-things-every-java-developer-should-stop-doing/"><![CDATA[<p>My friends <a href="https://andrematheus.net.br">Andre</a> and <a href="https://github.com/leonnardo">Leonnardo</a> have sent me an interesting <a href="https://dzone.com/articles/3-things-every-java-developer-should-stop-doing">article</a> about some bad habits every Java developer should stop doing in their code.</p>

<p>Basically the author picked up the following points and discussed each one of them, showing code examples why they aren’t good practices at all:</p>

<ol>
  <li>Returning Null</li>
  <li>Defaulting to Functional Programming</li>
  <li>Creating Indiscriminate Getters and Setters</li>
</ol>

<p>I agree with all these points as being bad practices in Java code. How about you? What do you think about it?</p>]]></content><author><name>Rafael Naufal</name></author><category term="code" /><category term="development" /><category term="functional" /><category term="java" /><category term="jdk8" /><category term="programming" /><category term="code" /><category term="development" /><category term="functional" /><category term="java" /><category term="jdk8" /><category term="programming" /><summary type="html"><![CDATA[My friends Andre and Leonnardo have sent me an interesting article about some bad habits every Java developer should stop doing in their code.]]></summary></entry><entry><title type="html">Java 8: Converting Optional Collection to the Streams API</title><link href="https://rafaelnaufal.com/2017/10/15/java-8-converting-optional-collection-to-the-streams-api/" rel="alternate" type="text/html" title="Java 8: Converting Optional Collection to the Streams API" /><published>2017-10-15T00:00:00+00:00</published><updated>2017-10-15T00:00:00+00:00</updated><id>https://rafaelnaufal.com/2017/10/15/java-8-converting-optional-collection-to-the-streams-api</id><content type="html" xml:base="https://rafaelnaufal.com/2017/10/15/java-8-converting-optional-collection-to-the-streams-api/"><![CDATA[<p>Although <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java 9</a> has already been released, this post is about converting an optional collection to the <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html">Streams API</a> introduced in <a href="http://www.oracle.com/technetwork/pt/java/javase/downloads/jdk8-downloads-2133151.html">Java 8</a>.</p>

<p>Suppose some person could have zero, one or more cars and it is represented by the <code class="language-plaintext highlighter-rouge">Person</code> class below (some code omitted).</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Person</span> <span class="o">{</span>

    <span class="kd">private</span> <span class="nc">String</span> <span class="n">name</span><span class="o">;</span>

    <span class="o">.</span>
    <span class="o">.</span>
    <span class="o">.</span>

    <span class="kd">public</span> <span class="nc">Optional</span><span class="o">&lt;</span><span class="nc">Collection</span><span class="o">&lt;</span><span class="nc">Car</span><span class="o">&gt;&gt;</span> <span class="nf">getCars</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="nc">Optional</span><span class="o">.</span><span class="na">ofNullable</span><span class="o">(</span><span class="n">cars</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="o">.</span>
    <span class="o">.</span>
    <span class="o">.</span>

<span class="o">}</span>
</code></pre></div></div>

<p>Now we create a list of people and we want to get Mark’s cars.</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Person</span> <span class="n">mark</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Person</span><span class="o">(</span><span class="s">"Mark"</span><span class="o">);</span>

<span class="nc">List</span><span class="o">&lt;</span><span class="nc">Person</span><span class="o">&gt;</span> <span class="n">people</span> <span class="o">=</span> <span class="o">...</span>
</code></pre></div></div>

<p>How can we do that using the Streams API, since the <code class="language-plaintext highlighter-rouge">getCars()</code> method return an <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html">Optional</a>?</p>

<p>One possibility is to filter people’s list by Mark’s name, filter the <code class="language-plaintext highlighter-rouge">Optional</code> if it is present or not and map its wrapped value (our cars list):</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Collection</span><span class="o">&lt;</span><span class="nc">Car</span><span class="o">&gt;</span> <span class="n">markCars</span> <span class="o">=</span> <span class="n">people</span>
                <span class="o">.</span><span class="na">stream</span><span class="o">()</span>
                <span class="o">.</span><span class="na">filter</span><span class="o">(</span><span class="n">person</span> <span class="o">-&gt;</span> <span class="s">"Mark"</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">person</span><span class="o">.</span><span class="na">getName</span><span class="o">()))</span>
                <span class="o">.</span><span class="na">findFirst</span><span class="o">()</span>
                <span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="nl">Person:</span><span class="o">:</span><span class="n">getCars</span><span class="o">)</span>
                <span class="o">.</span><span class="na">filter</span><span class="o">(</span><span class="nl">Optional:</span><span class="o">:</span><span class="n">isPresent</span><span class="o">)</span>
                <span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="nl">Optional:</span><span class="o">:</span><span class="n">get</span><span class="o">)</span>
                <span class="o">.</span><span class="na">orElse</span><span class="o">(</span><span class="nc">Collections</span><span class="o">.</span><span class="na">emptyList</span><span class="o">());</span>
</code></pre></div></div>

<p>At this moment we reached the reason of this blog post. And how can we get all people’s cars? The idea here is to use the <code class="language-plaintext highlighter-rouge">flatMap()</code> operation unwrapping the <code class="language-plaintext highlighter-rouge">Optional</code> to the collection’s stream when it is present or getting an empty stream when it isn’t present:</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Collection</span><span class="o">&lt;</span><span class="nc">Car</span><span class="o">&gt;</span> <span class="n">allPeopleCars</span> <span class="o">=</span> <span class="n">people</span>
                <span class="o">.</span><span class="na">stream</span><span class="o">()</span>
                <span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="nl">Person:</span><span class="o">:</span><span class="n">getCars</span><span class="o">)</span>
                <span class="o">.</span><span class="na">flatMap</span><span class="o">(</span><span class="n">mayHaveCars</span> <span class="o">-&gt;</span> <span class="n">mayHaveCars</span><span class="o">.</span><span class="na">isPresent</span><span class="o">()</span> <span class="o">?</span> <span class="n">mayHaveCars</span><span class="o">.</span><span class="na">get</span><span class="o">().</span><span class="na">stream</span><span class="o">()</span> <span class="o">:</span> <span class="nc">Stream</span><span class="o">.</span><span class="na">empty</span><span class="o">())</span>
                <span class="o">.</span><span class="na">collect</span><span class="o">(</span><span class="nc">Collectors</span><span class="o">.</span><span class="na">toList</span><span class="o">());</span>
</code></pre></div></div>

<p>We can do better and replace the above solution to be more functional using <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html">method references</a>:</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Collection</span><span class="o">&lt;</span><span class="nc">Car</span><span class="o">&gt;</span> <span class="n">allPeopleCars</span> <span class="o">=</span> <span class="n">people</span>
                <span class="o">.</span><span class="na">stream</span><span class="o">()</span>
                <span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="nl">Person:</span><span class="o">:</span><span class="n">getCars</span><span class="o">)</span>
                <span class="o">.</span><span class="na">flatMap</span><span class="o">(</span><span class="n">mayHaveCars</span> <span class="o">-&gt;</span> <span class="n">mayHaveCars</span><span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="nl">Collection:</span><span class="o">:</span><span class="n">stream</span><span class="o">).</span><span class="na">orElse</span><span class="o">(</span><span class="nc">Stream</span><span class="o">.</span><span class="na">empty</span><span class="o">()))</span>
                <span class="o">.</span><span class="na">collect</span><span class="o">(</span><span class="nc">Collectors</span><span class="o">.</span><span class="na">toList</span><span class="o">());</span>
</code></pre></div></div>

<p>If you use <a href="https://www.jetbrains.com/idea/">IntelliJ IDEA</a> as your favorite IDE, it has an inspection that helps replacing <code class="language-plaintext highlighter-rouge">Optional.isPresent()</code> with a functional expression:</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Collection</span><span class="o">&lt;</span><span class="nc">Car</span><span class="o">&gt;</span> <span class="n">allPeopleCars</span> <span class="o">=</span> <span class="n">people</span>
                <span class="o">.</span><span class="na">stream</span><span class="o">()</span>
                <span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="nl">Person:</span><span class="o">:</span><span class="n">getCars</span><span class="o">)</span>
                <span class="o">.</span><span class="na">flatMap</span><span class="o">(</span><span class="n">mayHaveCars</span> <span class="o">-&gt;</span> <span class="n">mayHaveCars</span><span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="nl">Collection:</span><span class="o">:</span><span class="n">stream</span><span class="o">).</span><span class="na">orElseGet</span><span class="o">(</span><span class="nl">Stream:</span><span class="o">:</span><span class="n">empty</span><span class="o">))</span>
                <span class="o">.</span><span class="na">collect</span><span class="o">(</span><span class="nc">Collectors</span><span class="o">.</span><span class="na">toList</span><span class="o">());</span>
</code></pre></div></div>

<p><strong>P.S.</strong> In Java 9, the <a href="http://download.java.net/java/jdk9/docs/api/java/util/Optional.html#stream--">stream()</a> method was added to the <a href="http://download.java.net/java/jdk9/docs/api/java/util/Optional.html">Optional</a> API, so we can rewrite the above stream pipeline into the following one:</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Collection</span><span class="o">&lt;</span><span class="nc">Car</span><span class="o">&gt;</span> <span class="n">allPeopleCars</span> <span class="o">=</span> <span class="n">people</span>
                <span class="o">.</span><span class="na">stream</span><span class="o">()</span>
                <span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="nl">Person:</span><span class="o">:</span><span class="n">getCars</span><span class="o">)</span>
                <span class="o">.</span><span class="na">flatMap</span><span class="o">(</span><span class="nl">Optional:</span><span class="o">:</span><span class="n">stream</span><span class="o">)</span>
                <span class="o">.</span><span class="na">flatMap</span><span class="o">(</span><span class="nl">Collection:</span><span class="o">:</span><span class="n">stream</span><span class="o">)</span>
                <span class="o">.</span><span class="na">collect</span><span class="o">(</span><span class="nc">Collectors</span><span class="o">.</span><span class="na">toList</span><span class="o">());</span>
</code></pre></div></div>

<p>In case you are interested, this <a href="https://blog.jetbrains.com/idea/2016/07/java-8-top-tips/">post</a> on the IntelliJ IDEA blog has some good tips when working with Java 8.</p>]]></content><author><name>Rafael Naufal</name></author><category term="collectors" /><category term="development" /><category term="idea" /><category term="intellij" /><category term="java" /><category term="java8" /><category term="jdk8" /><category term="jdk9" /><category term="optional" /><category term="programming" /><category term="streams" /><category term="collectors" /><category term="development" /><category term="idea" /><category term="intellij" /><category term="java" /><category term="java8" /><category term="jdk8" /><category term="jdk9" /><category term="optional" /><category term="programming" /><category term="streams" /><summary type="html"><![CDATA[Although Java 9 has already been released, this post is about converting an optional collection to the Streams API introduced in Java 8.]]></summary></entry><entry><title type="html">About the Kotlin programming language</title><link href="https://rafaelnaufal.com/2017/10/14/about-the-kotlin-programming-language/" rel="alternate" type="text/html" title="About the Kotlin programming language" /><published>2017-10-14T00:00:00+00:00</published><updated>2017-10-14T00:00:00+00:00</updated><id>https://rafaelnaufal.com/2017/10/14/about-the-kotlin-programming-language</id><content type="html" xml:base="https://rafaelnaufal.com/2017/10/14/about-the-kotlin-programming-language/"><![CDATA[<p>Kotlin is a statically typed language which is fully interoperable with <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java</a>.</p>

<p>Recently my friend <a href="https://andrematheus.net.br">Andre</a> showed me <a href="https://kotlinlang.org/">Kotlin</a>‘s nice syntax and I considered giving it a try.</p>

<p>In the meantime, my friend <a href="https://github.com/leonnardo">Leonnardo</a> sent me this nice <a href="https://fabiomsr.github.io/from-java-to-kotlin/">link</a> which helps migrating from Java to Kotlin easily.</p>

<p>Let’s compare some syntax from Java and Kotlin and see the differences. Suppose we have some employees and we want to group them by their departments.</p>

<p>In <strong>Java</strong> we create an <code class="language-plaintext highlighter-rouge">Employee</code> class, build some employees and use the <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html">Streams</a> API to group them by their departments:</p>

<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Employee</span> <span class="o">{</span>

    <span class="kd">private</span> <span class="kd">final</span> <span class="nc">String</span> <span class="n">name</span><span class="o">;</span>

    <span class="kd">private</span> <span class="kd">final</span> <span class="nc">String</span> <span class="n">department</span><span class="o">;</span>

    <span class="kd">public</span> <span class="nf">Employee</span><span class="o">(</span><span class="kd">final</span> <span class="nc">String</span> <span class="n">name</span><span class="o">,</span> <span class="kd">final</span> <span class="nc">String</span> <span class="n">department</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="na">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">;</span>
        <span class="k">this</span><span class="o">.</span><span class="na">department</span> <span class="o">=</span> <span class="n">department</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="nc">String</span> <span class="nf">getName</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">name</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="nc">String</span> <span class="nf">getDepartment</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">department</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="nc">String</span> <span class="nf">toString</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="s">"Employee(name="</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">", department="</span> <span class="o">+</span> <span class="n">department</span> <span class="o">+</span> <span class="s">")"</span><span class="o">;</span>
    <span class="o">}</span>
<span class="o">}</span>

<span class="kd">final</span> <span class="nc">Employee</span> <span class="n">mark</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Employee</span><span class="o">(</span><span class="s">"Mark"</span><span class="o">,</span> <span class="s">"Accounting"</span><span class="o">);</span>
<span class="kd">final</span> <span class="nc">Employee</span> <span class="n">john</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Employee</span><span class="o">(</span><span class="s">"John"</span><span class="o">,</span> <span class="s">"Management"</span><span class="o">);</span>
<span class="kd">final</span> <span class="nc">Employee</span> <span class="n">smith</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Employee</span><span class="o">(</span><span class="s">"Smith"</span><span class="o">,</span> <span class="s">"Administrative"</span><span class="o">);</span>
<span class="kd">final</span> <span class="nc">Employee</span> <span class="n">paul</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Employee</span><span class="o">(</span><span class="s">"Paul"</span><span class="o">,</span> <span class="s">"Accounting"</span><span class="o">);</span>

<span class="kd">final</span> <span class="nc">List</span><span class="o">&lt;</span><span class="nc">Employee</span><span class="o">&gt;</span> <span class="n">employees</span> <span class="o">=</span> <span class="nc">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="n">mark</span><span class="o">,</span> <span class="n">john</span><span class="o">,</span> <span class="n">smith</span><span class="o">,</span> <span class="n">paul</span><span class="o">);</span>

<span class="kd">final</span> <span class="nc">Map</span><span class="o">&lt;</span><span class="nc">String</span><span class="o">,</span> <span class="nc">Employee</span><span class="o">&gt;</span> <span class="n">employeesByDepartment</span> <span class="o">=</span> <span class="n">employees</span>
                <span class="o">.</span><span class="na">stream</span><span class="o">()</span>
                <span class="o">.</span><span class="na">collect</span><span class="o">(</span><span class="nc">Collectors</span><span class="o">.</span><span class="na">groupingBy</span><span class="o">(</span><span class="nl">Employee:</span><span class="o">:</span><span class="n">getDepartment</span><span class="o">));</span>
</code></pre></div></div>

<p>In <strong>Kotlin</strong> we create a data class <code class="language-plaintext highlighter-rouge">Employee</code>, build some employees and use the collection built-in <code class="language-plaintext highlighter-rouge">groupBy</code> method to group them by their departments:</p>

<div class="language-kotlin highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">data class</span> <span class="nc">Employee</span><span class="p">(</span><span class="kd">val</span> <span class="py">name</span><span class="p">:</span> <span class="nc">String</span><span class="p">,</span> <span class="kd">val</span> <span class="py">department</span><span class="p">:</span> <span class="nc">String</span><span class="p">)</span>

<span class="kd">val</span> <span class="py">mark</span> <span class="p">=</span> <span class="nc">Employee</span><span class="p">(</span><span class="s">"Mark"</span><span class="p">,</span> <span class="s">"Accounting"</span><span class="p">)</span>
<span class="kd">val</span> <span class="py">john</span> <span class="p">=</span> <span class="nc">Employee</span><span class="p">(</span><span class="s">"John"</span><span class="p">,</span> <span class="s">"Management"</span><span class="p">)</span>
<span class="kd">val</span> <span class="py">smith</span> <span class="p">=</span> <span class="nc">Employee</span><span class="p">(</span><span class="s">"Smith"</span><span class="p">,</span> <span class="s">"Administrative"</span><span class="p">)</span>
<span class="kd">val</span> <span class="py">paul</span> <span class="p">=</span> <span class="nc">Employee</span><span class="p">(</span><span class="s">"Paul"</span><span class="p">,</span> <span class="s">"Accounting"</span><span class="p">)</span>

<span class="kd">val</span> <span class="py">employees</span> <span class="p">=</span> <span class="nf">listOf</span><span class="p">(</span><span class="n">mark</span><span class="p">,</span> <span class="n">john</span><span class="p">,</span> <span class="n">smith</span><span class="p">,</span> <span class="n">paul</span><span class="p">)</span>

<span class="kd">val</span> <span class="py">employeesByDepartment</span> <span class="p">=</span> <span class="n">employees</span><span class="p">.</span><span class="nf">groupBy</span> <span class="p">{</span> <span class="n">it</span><span class="p">.</span><span class="n">department</span> <span class="p">}</span>
</code></pre></div></div>

<p>As you can see, Kotlin has some syntactic sugar that makes it less verbose than Java.</p>

<p>If you haven’t considered trying Kotlin yet, I think it is worth giving it a try.</p>]]></content><author><name>Rafael Naufal</name></author><category term="collectors" /><category term="development" /><category term="java" /><category term="java8" /><category term="kotlin" /><category term="programming" /><category term="streams" /><category term="collectors" /><category term="development" /><category term="java" /><category term="java8" /><category term="kotlin" /><category term="programming" /><category term="streams" /><summary type="html"><![CDATA[Kotlin is a statically typed language which is fully interoperable with Java.]]></summary></entry></feed>