Java

How to make Eclipse run with a custom JDK on Mac

You might want to have more than one JDK on your Mac and run different programs with different JDK versions as it is with me. The easiest and safest way I’ve found is as follows. First, download the JDK archive (not the DMG installation file) from Oracle, for example version 11: https://www.oracle.com/java/technologies/javase-jdk11-downloads.html Note: you’ll need… read more »

How to make Hazelcast’s cluster replication more resilient

Hazelcast is an open source in-memory data grid. It is written in Java and naturally it is popular for Java applications but not only. In simple words, Hazelcast is like a shared map which you can use across different web applications, each of which might be situated on different network nodes and use this map… read more »

How to create a Singleton class properly in Java

A Singleton class is a class of which only one instance exists. There is a need for such a design pattern for loggers, database connections and other scenarios. The main challenge comes when you want to implement a Singleton in a mutli-threaded environment and ensure that all the threads are synchronised when creating the Singleton… read more »

What are concurrency problems and how to avoid them in Java

Concurrency problems appear when your code is executed by more than one thread. Then, in contrast to a single-threaded execution, your code might behave differently depending on when and which thread accesses a variable. Here is an example: In the above code: There is a static variable counter starting from 0. A fixed thread pool… read more »

Period, Duration and Instant in Java

Period, Duration and Instant are three different classes introduced since Java 8 to help us deal better with time. Period measures the time with days granularity. In other words, there are no hours, minutes, nor seconds. Example: There are also methods ofDays, ofMonths and so on. It’s noteworthy to mention that you cannot convert automatically… read more »

Why and How to Use Optional in Java

The Optional object type in Java was introduced with version 8 of Java. It is used when we want to express that a value might not be known (yet) or it’s not applicable at this moment. Before Java 8 developers might have been tempted to return a null value in this case. Ultimately, there are… read more »

Why and How to Use Java.util.stream.Collectors

Streams are a wonderful feature in Java allowing you to write clean, efficient and powerful code. If you haven’t consumed the output of the stream, you will probably want to collect it. That’s when the methods from Java.util.stream.Collectors come to help. Here are some useful examples: Collect to list: We start with a list of… read more »

Java Built-In Functional Interfaces Cheatsheet and Examples

In order to use lambda expressions in Java 8, you need a functional interface. For most of your needs, you can use the already built ones in Java which are as follows: Supplier – generate output without taking any input. For example, get the current date: Then use getDate to assign a LocalDate variable: Consumer… read more »

Java Generics With Upper and Lower Bounds Simply Explained

Generics in Java and especially such with upper and lower bounds can be confusing even though they are otherwise very useful. Here is an example which explains it all well: We have two classes – A and B. B extends A. We’ll use them to test the upper and lower bounds of the wildcard generics… read more »

Avoid the Telescoping Anti-pattern in Java

The Telescoping anti-pattern is widely-spread, just like a virus. It starts with good intentions. For example, a class that has two properties like this: The above constructor starts being used for creating new Example objects. At one point, the project requirements change and a third parameter is needed. For example, let’s assume that the above… read more »

Sidebar