One of the coolest things that where introduced in Java 8 was the Optional API. I've earlier written about how Optional can clean up your code. In short terms, Optional is a container object that may or may not contain a non-null value. It also offers a great range of…
Java 10 is just around the corner with a GA set to March. One of the biggest news is the Local-Variable Type Inference, which brings improvements to how you're declaring your variables. Let me explain. Looking at other languages you often see variable declarations without the type specified. In JavaScript…
If you've worked with streams you've probably played around with the Collectors class. In earlier posts I've looked at how you can accumulate elements of a stream using Collectors as well as looking at how you can create a custom collector. In this post we're going to take a look…
When writing asynchronous code using CompletableFuture you can quickly end up in a scenario where you can't wait for a given task to finish. You simply need to timeout and move on. This is something that has been addressed in Java 9 by introducing two new functions — orTimeout() and completeOnTimeout(…
Working with collections in Java has always been a bit of a hassle — especially if you want to initialise the collection and add data to it in one go. Where other languages got handy one-liners, the java coder had a way more cumbersome job ahead by first having to initialise…
From time to time you end up with the need of generating some random numbers. In Java, the Random class has been the go-to solution for this type of work. Random serves several helpful methods for creating random ints, doubles and longs. However, if you wanted a sequence of random…
Java 8 brought huge changes — one of them being the default interface methods. These methods changed interfaces as we knew them — we could now define default implementations in the interface itself. If you use these default methods heavily in your applications, you'll quickly figure out one thing — it doesn't really…
One of the first things I check out when looking at a new language is if it got a good collection API. I find that a good way of working with collections usually result in cleaner code that is easier to understand and reason about. That's why I was glad…