Recently I gave a talk that included a slide where I briefly discussed single return vs multiple returns.
The purpose of this slide was only to give an example of a dogma and basically had nothing to do with the rest of the talk.
Therefore it was kinda funny to see that this particular slide caused a lot of discussion afterwards, so it seems natural to write a blog about this topic.
So… should a function only have a single return statement or is it better to allow multiple returns?
Continue reading →
If we have an Optional
instance we can consume the value if it is present using the ifPresent
method. Since Java 9 the method ifPresentOrElse
has been added to the Optional
class. The first argument is of type Consumer
and is invoked when there is an optional value. The second argument is of type Runnable
and is executed when the the optional is empty. The method in the Consumer
and Runnable
implementations does not return a type but returns void
. Therefore we should use ifPresentOrElse
when we need a conditional side effect for an Optional
instance.
In the following example we have a method handleName
that will update a list if an optional value is present or increases a counter when the optional value is empty:
Continue reading →
Outside the Java community, Java is often regarded as an old and verbose language. Though I love writing Java code, I kind of have to agree with this. New features are implemented slowly and looked upon by the language designers with thorough suspicion. For example, support for multi-line strings has been tried multiple times before Java got official support. If we are talking about verbosity, the Java language needs quite some characters to write a simple function. As I am specializing in functional programming in Java this year, I struggled a lot with this. Read along how I tackled this a little.
Continue reading →
If we want to get two types of information from a Stream
of objects we can consume the Stream
twice and collect the results. But that is not very efficient, especially when the stream has a lot of objects. Since Java 12 we can use the teeing
method of the java.util.stream.Collectors
class to get multiple results while consuming the stream of objects only once. The teeing
method takes two collectors as argument each returning a separate result for the stream items. As third argument we must pass a function that will merge the results of the two collectors into a new object.
In the following code we have two example use cases that use the teeing
method to get multiple results while consuming a Stream
of objects only one time:
Continue reading →
Our team has a (not so) slight tendency to not immediately follow through with our deployments to production.
We’ll create and review our changes, merge and deploy to staging, and dilligently test the changes there.
And then… nothing happens.
It could be that something else needs our immediate attention, or someone else wants to confirm an issue is fixed;
Or we might want to deploy at a different point in time as to not disrupt an ongoing process by a service restart.
Any which way the result is the same: changes accumulate in staging, and with that the risk involved with the next production deployment.
To nudge ourselves to deploy to production more often we created a Slack App that gives us a daily report of such pending deployments.
In this post I’ll showcase the code we use, and how to set up something similar yourself.
Continue reading →
Java 15 introduced the multi-line string value referred to as a text block. With this introduction also the formatted
method was added to the String
class. The method can be invoked on a String
value directly and function exactly as the static String.format
method. The nice thing is that now we directly can use a method on the value instead of having to use a static method where the value is passed as argument.
In the following example we use the formatted
method for a normal String
value and a text block:
Continue reading →
In Java 12 the transform
method was add to the String
class. This method accepts a Function
as argument. The function must have a single parameter of type String
and can return any other type. The nice thing is that it works on a String
instance, so we can directly use the transform
method when we have a String
value. We don’t have to pass the String
object to another method to tranform it, but we can define the tranformation function close to the String
value.
In the following example we take a String
value and apply some functions with the transform
method:
Continue reading →
Since Java 12 we can format numbers in a compact style with the CompactNumberFormat
class in the java.text
package. A number like 23000 is formatted as 23K for the English locale. Instead of the short representation of K for 1000 we can also use a longer style where K is transformed as thousand for the English locale. We can use the same class to parse a String value that is in the compact style into a number.
In the following example we use several options of the CompactNumberFormat
class:
Continue reading →
In Java we can merge a key/value pair into a Map
with the merge
method. The first parameter is the key, the second the value and the third parameter of the merge
method is a remapping function that is applied when the key is already present in the Map
instance. The remapping function has the value of the key in the original Map
and the new value. We can define in the function what the resulting value should be. If we return null
the key is ignored.
If we want to merge multiple Map
instances we can use the Stream API. We want to convert the Map
instances to a stream of Map.Entry
instances which we then turn into a new Map
instance with the toMap
method from the class Collectors
. The toMap
method also takes a remapping function when there is a duplicate key. The function defines what the new value is based on the two values of the duplicate key that was encountered. We can choose to simply ignore one of the values and return the other value. But we can also do some computations in this function, for example creating a new value using both values.
Continue reading →
In Java we can write single argument functions that implement the java.util.function.Function
interface. We can combine multiple functions into a new function using the andThen
and compose
methods from the Function
interface. We need to give another function as argument to these methods. When we use the andThen
method the output of the original function will be input of the function passed as argument. With the compose
method our function will get as input the output of the function that is passed as argument. It is important to know the difference, because it can change the result of the function we are composing. The andThen
and compose
methods are also available on the IntUnaryOperator
, LongUnaryOperator
and DoubleUnaryOperator
interface.
In the following example we use both andThen
and compose
to chain together some functions. We can see the result can be different when using andThen
and compose
with the same functions.
Continue reading →
The Java Stream API has many useful methods. If we want to partition a stream of objects by a given predicate we can use the partitioningBy()
method from the java.util.stream.Collectors
package. We must use this method in the collect()
method of the stream. The result is a Map
with the keys true
and false
. The objects from the stream that are true for the predicate will end up in the true
value list and if the result of the predicate is false
the value will end up in the list of values for the false
key. The partitionBy
method accepts a collector as second parameter. This collector will be applied to the values before they are put in the true
or false
keys in the result.
In the following example we use the partitioningBy
method with different streams:
Continue reading →
The Java Stream
API has many useful methods. If we want to transform a Stream
to a Java array we can use the toArray
method. Without an argument the result is an object array (Object[]
), but we can also use an argument to return an array of another type. The easiest way is to use the contructor of the array type we want as method reference. Then the result is an array of the given type with the elements of the stream.
This is very useful if we have a Java Stream
and want to use the elements to invoke a method with a variable arguments parameter. In Java we can pass an array object as variable arguments argument to a method. So if we transform the Stream
to an array we can invoke the method with that value.
Continue reading →