How to Create Immutable Objects and Properties in Java

Being able to create an immutable object in Java is one of the most essential skills for any Java programmer. Such objects are needed when you want to rest assured that a created object cannot change for whatever reason. The common approach is to create an object with private final fields and allow only getters… read more »

How to Use a Functional Interface in Java

A functional interface is an interface that contains a single abstract method. It is used as the basis for lambda expressions in functional programming. Such an interface may contain also other non-abstract methods even though this is not considered a good practice. Also, the notation @FunctionalInterface is optional but it will ensure that the intention… read more »

Use Lambdas Instead of Anonymous Classes In Java

Another useful feature introduced since Java 8 is the possibility to use lambdas instead of anonymous classes. Here is an example: Imagine you have the following list of names: If you want to sort it alphabetically ignoring the case, you would have to use a Comparator and implement its compare method in an anonymous class… read more »

Java 8 Streams, Functions and Reductions Explained

There is a relatively simple challenge to extend the vowels in a string. Here is a way to do it in Java 8: In the above I’ve tried to make the code as short as possible while still ensuring it’s readable and conforming to the clean code rules. All of that could be written without… read more »

Reducing Streams in Java

Java 8 introduced streams, among other things, and one of my favourite features is the reduce() method. It combines a stream into a single method. Before Java 8 and streams, such a reduction had to be done with a loop like this: Now with Streams and reduce() it can be done as follows with a… read more »

Delete Files with Java 8

A friend asked me to help him with the following in Bash – delete all files but a whitelisted and use mix / max depth for directory traversal. It’s probably possible in Bash with some crazy find, grep, etc one-liner. But here’s how good it looks in Java 8 with streams, predicates, etc… With Java… read more »

How to Create a Simple Honeypot with Spring Boot

I am sure that there are plenty of honeypots for logging remote intruders but none yet has been done on Spring Boot. So I decided to write a simple one while exploring Spring Boot: It does the following – listens on a preconfigured port. Define it in the application properties (src/main/resources/ as blocker.honeypot_port. By… read more »

How to Create a Simple Mockito Unit Test

Mockito is a popular Java framework which comes to great help in Unit testing. In Unit testing it’s important to get to a state where you can concentrate on testing a specific functionality or behaviour. Mockito can help you with this by mocking or simulating specific methods which you may need. It’s important to be… read more »

Connect to Elasticsearch with Java and RestHighLevelClient

Connecting to Elasticsearch and executing queries is generally considered easy. In fact, in many cases a simple curl query like this one is all you need: Though, if you want something more than just finding all documents that contain mySearch (as per the example above), you may get lost in the abundance of options and… read more »

Build A Java Project With Maven

Using Maven to maintain and build your Java projects has a lot of advantages including automatically resolving dependencies, running tests and packaging. In this article you will learn about a complete development cycle using Maven from creating the project to building it. Installing Maven You can install Maven on most Linux distributions using the default… read more »