Home » Archives for Fahd Shariff

Author Archives: Fahd Shariff

Fahd is a software engineer working in the financial services industry. He is passionate about technology and specializes in Java application development in distributed environments.

Java 17: Pattern Matching for Switch

In Java 17 (released only a few days ago), Pattern Matching for switch has been introduced as a preview language feature, which allows case labels with patterns rather than just constants. Here is an example showing how you can match on type patterns: public static String typedPatternMatching(Object o) { return switch(o) { case null -> "I am null"; case String ...

Read More »

Java 16: Stream.mapMulti

Java 16 introduces a new Stream.mapMulti method which allows you to replace elements in a stream with multiple elements. The example below shows how you can use mapMulti to replace each string in a stream with its uppercased and lowercased versions: Stream.of("Twix", "Snickers", "Mars") .mapMulti((s, c) -> { c.accept(s.toUpperCase()); c.accept(s.toLowerCase()); }) .forEach(System.out::println); Output: TWIX twix SNICKERS snickers MARS mars The ...

Read More »

Java 16: Stream.toList()

Java 16 introduces a handy new Stream.toList() method which makes it easier to convert a stream into a list. The returned list is unmodifiable and calls to any mutator method will throw an UnsupportedOperationException. Here is some sample code: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 import java.util.stream.Stream; import static java.util.stream.Collectors.*; ...

Read More »

kdb+/q – Display a Table as a Tree

This post shows how you can convert a keyed table to a hierarchical tree format in kdb+/q. This could be useful if you want to display data as a tree widget in a front-end. Consider the following keyed table of world populations: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 continent     country        city            | ...

Read More »

Creating Temporary Files with JUnit 5

This post shows you how to perform unit testing using temporary files with JUnit 5. If you’re still on JUnit 4, please check out my previous post! In JUnit 5, the @TempDir annotation is used to indicate that a field or method parameter of type Path or File is a temporary directory. Each test will use its own temporary directory ...

Read More »

Parameterized Tests in JUnit 5

A parameterized test allows you to run a test against a varying set of data. If you find yourself calling the same test but with different inputs, over and over again, a parameterized test would help make your code cleaner. To create one in JUnit 5 you need to: Annotate the test method with @ParameterizedTest Annotate the test method with ...

Read More »

Testing Expected Exceptions with JUnit 5

This post shows how to test for expected exceptions using JUnit 5. If you’re still on JUnit 4, please check out my previous post. Let’s start with the following class that we wish to test: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 public class Person {   private final ...

Read More »

Java 15: Sealed Classes

Java 15 introduces Sealed Classes, a preview language feature, that allows classes/interfaces to restrict which other classes/interfaces may extend or implement them. Here is an example: 1 2 3 4 5 public sealed class Vehicle permits Car, Truck, Motorcycle { ... }   final class Car extends Vehicle { ... } final class Truck extends Vehicle { ... } final ...

Read More »

kdb+/q – Try Catch

Programming languages typically have a try-catch mechanism for dealing with exceptions. The try block contains the code you want to execute and the catch block contains the code that will be executed if an error occurs in the try block. Here is an example of a simple try-catch block in Java, which attempts to parse a string into an int ...

Read More »