Posts Tagged development

Java 8: Converting Optional Collection to the Streams API

Although Java 9 has already been released, this post is about converting an optional collection to the Streams API introduced in Java 8.

Suppose some person could have zero, one or more cars and it is represented by the Person class below (some code omitted).

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

How can we do that using the Streams API, since the getCars() method return an Optional?

One possibility is to filter people’s list by Mark’s name, filter the Optional if it is present or not and map its wrapped value (our cars list):

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 flatMap() operation unwrapping the Optional to the collection’s stream when it is present or getting an empty stream when it isn’t present:

We can do better and replace the above solution to be more functional using method references:

If you use IntelliJ IDEA as your favorite IDE, it has an inspection that helps replacing Optional.isPresent() with a functional expression:

P.S. In Java 9, the stream() method was added to the Optional API, so we can rewrite the above stream pipeline into the following one:

In case you are interested, this post on the IntelliJ IDEA blog has some good tips when working with Java 8.

Tags: , , , , , , , , , ,

About the Kotlin programming language

Kotlin is a statically typed language which is fully interoperable with Java.

Recently my friend Andre showed me Kotlin‘s nice syntax and I considered giving it a try.

In the meantime, my friend Leonnardo sent me this nice link which helps migrating from Java to Kotlin easily.

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.

In Java we create an Employee class, build some employees and use the Streams API to group them by their departments:

In Kotlin we create a data class Employee, build some employees and use the collection built-in groupBy method to group them by their departments:

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

If you haven’t considered trying Kotlin yet, I think it is worth giving it a try.

Tags: , , , , , ,

35 programming habits that make your code smell

Interesting article about some bad programming practices, including topics like code organization, teamwork, testing and maintenance.

Tags: , , , ,

Thinking Iterative, but doing Waterfall development

Some companies think they’re applying iterative and incremental development, but in practice are doing waterfall development. Here are some tips described in Applying UML and Patterns, by Craig Larman, which can help you discover if your company doesn’t understand the Unified Process and iterative development (these tips can clarify all stakeholders misunderstanding about the difference of the two approaches):

  1. You think that inception = requirements, elaboration = design, and construction = implementation (that is, superimposing a waterfall lifecycle onto the UP).
  2. You think that the purpose of elaboration is to fully and carefully define models, which are translated into code during construction.
  3. You try to define most of the requirements before starting design or implementation.
  4. You try to define most of the design before starting implementation; you try to fully define and commit to an architecture before iterative programming and testing.
  5. A “long time” is spent doing requirements or design work before programming starts.
  6. You believe that a suitable iteration length is four months long, rather than four weeks long (excluding projects with hundreds of developers).
  7. You think UML diagramming and design activities are a time to fully and accurately define designs and models in great detail, and of programming as a simple mechanical translation of these into code.
  8. You think that adopting the UP means to do many of the possible activities and create many documents, and thinks of or experiences the UP as a formal, fussy process with many steps to be followed.
  9. You try to plan a project in detail from start to finish; you try to speculatively predict all the iterations, and what should happen in each one.
  10. You want believable plans and estimates for projects before the elaboration phase is finished.
  11. Are you involved in such a situation at your company?

    Tags: , ,