Welcome to Knowledge Basement

Linux, Java, Open Source related how-to articles

Introduction to Tengine Web Server

Tengine seems to be a popular web server for Chinese websites. I’ve come upon it while browsing through Aliexpress and stumbling upon a 404 error: This error and more specifically its footer “Powered by Tengine” stirred up my interest and so I started looking for more information about this unknown to me web server. Here… 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 »

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 »

Sidebar