My Articles:
Learn to convert a stream to an array and vice versa in Java. We will learn to convert for the primitives as well as the Object types. Note that Java Stream API provides the following specialized classes for the stream of primitives. These classes support many useful sequential and parallel …
Learn to remove the array items in Java by the index positions as well as by the item values. Note that theoretically, we can remove an array item in two ways: Create a new array and copy all items from the original array, except the index or item to be …
Learn to convert an array of primitives (int, long or double) to an array of objects (Integer, Double or Long), and vice versa. For example, we will convert int[] to Integer[] and then convert back the Integer[] to int[]. Note that all the techniques follow the same logic for the …
Learn to split an array in Java using different ways. We will learn to split the array into equal parts, at the specified index and of equal lengths. 1. Arrays.copyOfRange() API The copyOfRange() creates a new array of the same type as the original array, and contains the items of …
Learn to concatenate two primitive arrays or objects arrays to create a new array consisting of the items from both arrays. We will learn to merge array items using simple for-loop, stream API and other utility classes. Note that no matter which technique we use for merging the arrays, it …
Learn to compare two arrays using different techniques in Java. We will learn the array comparison from using simple for loops to inbuilt Java APIs. 1. How is the Arrays Comparison Done? In Java or any other programming language, the basics behind comparing two arrays are the same. Two arrays …
Learn to check if a given array is already sorted for a defined sorting order i.e. ascending, descending or the custom order. 1. Overview An array is considered sorted if every item in the array is greater or lesser than its predecessor based on the sorting order of the array. …
Learn to find the top N items in a given array in Java. Note that we should be very clear about the meaning of top N items. The suggested solution may need minor changes based on our interpretation and requirement. For example, in this tutorial, top N items mean the …
Learn to find the smallest and the largest item in an array in Java. We will discuss different approaches from simple iterations to the Stream APIs. In the given examples, we are taking an array of int values. We can apply all the given solutions to an array of objects …
Arrays are fixed-size data structures and array sizes can not be changed once they have been initialized. However, in cases where array size needs to be changed, we have to follow one of the given approaches in this tutorial. 1. Using java.util.Arrays.copyOf() The copyOf(originalArray, newLength) method takes an array and …
Learn how to reverse or invert an array in Java. A reversed array is of equal size to the original array and contains the same items but in the reverse order. 1. Collections.reverse() API The easiest way to reverse the array is to use the existing APIs built for this …
Learn to declare and initialize arrays using different techniques and their differences. Apart from directly accessing the arrays, we will also be using the java.util.Arrays and Stream API that provides several useful methods to work with arrays in Java. Note that an array is a contiguous block of memory so …
Learn some popular and the most used Java unit testing frameworks used for mocking the test dependencies and verifying the test results.
Java is one of the most popular programming languages for developing all kinds of desktop applications, web applications and mobile apps. The following articles will help you quickly become familiar with Java language and move towards more complex concepts such as API and cloud development. 1. Java Language Basics Start …
Learn to sort a Java Set, List and Map of primitive types and custom objects using Comparator, Comparable and new lambda expressions. We will learn to sort in ascending and descending order as well. 1. Sorting a List of Objects To sort a list of objects, we have two popular …
SLF4J (Simple Logging Facade for Java) is a simple facade or abstraction for various logging frameworks, such as Java Util Logging (JUL), Logback and Log4j2. We use the abstract SLF4J API in the application, and later we can plug in any desired logging framework as underlying implementation. Using SLF4J helps …
A Mapped Diagnostic Context, or MDC in short, is an instrument for distinguishing interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously. 1. Stamping Requests with MDC MDC is used to stamp each request. It is done by putting the contextual …
Flogger, developed, used and recommended by Google, is a fluent style logging API for Java. Apart from fluency, flogger offers many more other features than existing logging frameworks that we will learn in this tutorial. 1. Flogger Dependencies Flogger, similar to SLF4J, acts as an abstraction and uses the underlying …
Masking sensitive data in logback logs is done by partially or fully replacing the client-sensitive data or NPI (nonpublic personal information) with some arbitrary encoded text. For example, the SSN information can be replaced with all star characters or we can remove the complete SSN information from the logs. 1. …
Logback XMLLayout outputs events into XML format that is log4j.dtd compliant (instead of an W3C XML Schema). The generated XML logs can be viewed using the opensource Log viewer tools such as Apache Chainsaw and vigilog. 1. XMLLayout Sample Configuration The given configuration logs into the rolling files using the …
Logback HTMLLayout outputs events in an HTML table. The content of the table columns is specified using a conversion pattern in the encoder property. 1. HTMLLayout Sample Configuration For quick reference, this is the configuration file we will be referring. It logs into c:/temp/logs/applicationLogs.html file and rolls-over daily. 2. Dependencies …
Logback RollingFileAppender appends log events into a file with the capability to rollover (archive the current log file and resume logging in a new file) based on a particular schedule, such as daily, weekly, monthly or based on log file size. For quick reference, this is configuration file we will …
Logback ConsoleAppender appends on the console though System.out or System.err print streams. In this Logback tutorial, learn about default configured console logging and how to apply custom configuration. 1. Dependencies Logback requires three modules in the application runtime i.e. logback-core, logback-classic and slf4j-api. The logback-classic module has implemented the SLF4J …
Logback is intended as a successor to the log4j project and was designed by Ceki Gülcü. Logback is faster and has a smaller footprint than all existing logging systems. Logback performs about ten times faster than Log4j on specific critical execution paths. The Logger class in logback-classic implements the SLF4J …
Recursion is a powerful algorithmic technique (divide-and-conquer strategy) in which a function calls itself (either directly or indirectly) on a smaller problem of the same type in order to simplify the problem to a solvable state. 1. What is Recursion? Recursion is an approach for the problems that can be …
Learn to add default accessor methods (getter and setter) using Lombok and omit these methods for specific requirements. 1. Default Behavior Lombok provides two annotations to add getter and setter methods to a POJO. @Getter – can be applied on any field and class. When applied on a field, lombok …
Learn to serialize and deserialize Lombok @Builder annotated class with Jackson library. For demo purpose, we are using Article class have few field members. We have created this class during the Lombok @Builder tutorial. 1. Serializing @Builder Class The serialization process is very simple and we do not do anything …
Lombok’s @Builder annotation is a useful technique to implement the builder pattern that aims to reduce the boilerplate code. In this tutorial, we will learn to apply @Builder to a class and other useful features. Ensure you have included Lombok in the project and installed Lombok support in the IDE. …
Learn to configure Log4j2 using Maven and Gradle dependencies. 1. Log4j Modules The main modules of interest are given below. We can read the complete list of modules and their dependencies at this link. 2. Maven Dependencies An example of log4j2 configuration with SLF4J using Maven. We can check the …
Learn to configure Log4j2 logging with SLF4J APIs. We will look at required dependencies, sample configuration and a demo to use the log statements. Read More: SLF4j Vs Log4j – Which One is Better? 1. Log4j2 and SLF4j Binding Dependencies To make Log4j2 work with SLF4J, we need to include …
Learn to sort a Java List or Stream with Comparator‘s nullsFirst() and nullsLast() methods. The Stream may contain null values, or the custom objects may have null field values. Failing to handle the null values during comparison will cause NullPointerException in runtime. 1. Introduction In this tutorial, we will learn …
Learn to configure the Log4j2 provided JDBCAppender that writes log events to a relational database table using standard JDBC. The JDBCAppender obtains the database connection from a configured connection factory or JNDI datasource. Then it uses the provided column configuration to insert a new row in the log table, everytime …
Java 17 was released on September 14, 2021. Java 17 is an LTS (Long Term Support) release, like Java 11 and Java 8. Oracle will support it for bug fixes, patches and performance enhancements for the next few years. Spring 6 and Spring boot 3 will have first-class support for …
JaCoCo is an excellent open-source toolkit to measure and capture code coverage when running JUnit tests. In this tutorial, we will learn to create test code coverage reports for JUnit 5 using the JaCoCo Maven Plugin. 1. JaCoCo Maven Plugin 1.1. JaCoCo JaCoCo is a code coverage library developed by …
Learn to create an HTML report for the test execution results using JUnit 5. In this example, we are creating an HTML report for JUnit-Examples project available on Github. 1. Maven Surefire Report Plugin To generate an HTML report, you can use the Maven Surefire Report Plugin, which converts the …
Learn to enable or disable the execution of the specific tests in JUnit 5 using inbuilt conditional execution annotations. These annotations run or skip tests based on environment configurations such as: The following table summarizes these annotations in more detail: Annotation Description Usage @EnabledOnOs@DisabledOnOs Enables/disables the test if the specified …
JUnit 5 supports test ordering using the @TestClassOrder and @TestMethodOrder annotations. They allow to sort tests by display names, in random and custom order.
The @Timeout annotation in JUnit 5 allows us to set a maximum time limit for a test or a method. If the test method exceeds the specified time limit, the test will fail automatically. This is useful when we want to ensure that a test does not run indefinitely, especially …
JUnit 5 @ParameterizedTest allows us to run the same test method (with multiple arguments) several times with different sets of input values.
Sometimes tests require temporary directories and files for performing intermediate test steps. JUnit 5 provides the @TempDir annotation that supplies a temporary directory to the test method or lifecycle methods. 1. JUnit @TempDir Annotation JUnit Jupiter provides an extension model that allows us to extend the behavior of the test …
Learn about String class in Java, the motivation behind making it immutable, and the idea of a String constant pool. We will see how memory is manipulated when we create String instances via String literals or String constructors. Finally, we will go through the main advantages and disadvantages caused by …
Learn to create Java enum where each enum constant may contain multiple values. We may use any of the values of the enum constant in our application code, and we should be able to get the enum constant from any of the values assigned to it. 1. How to Create …
Learn to add items to a Java Stream. Remember that a Stream is not a data structure or collection that can store values. To add items to an existing Stream, we need to : Create a new Stream with items to be added Concatenate with the first stream to get …
Learn to get an object with the latest date (max date) from a Stream of custom objects. We will use a custom Comparator for comparing the Date values stored in the custom objects. This example uses Employee class. We will create a program to get the youngest employee in a …
Learn to check if a given Date is a weekend in Java. We will learn to check using java.util.Date as well as Java 8 java.time.LocalDate classes. In the given examples, we are assuming that a weekend is either Saturday or Sunday. The rest five days of the week are weekdays. …
Since Spring 5, non-blocking Spring WebClient is a preferred alternative to blocking RestTemplate which is in maintenance mode/deprecated since then.
Python range type represents an immutable sequence of numbers. The most common use of a range is to use it for iterating over a series of items (list, set, tuple, dictionary or string). The range object is more memory efficient than list or tuple. A range stores only the start, …
Learn to deserialize JSON string and convert deserialized JSON data to a custom class (e.g. User) by extending JSONDecoder or object_hook method. In Python custom deserialization example, we have following User class. Here, birthdate of type datetime and we can define any formatter string for it. 1. Python Custom Deserializer …
Learn to serialize a complex python class (e.g. User) and write serialized JSON data in file by extending JSONEncoder and custom serialization method. In Python serialize complex object to JSON example, we have following User class. Here, birthdate of type datetime. If we don’t use the custom serialization, we will …
Learn to read a JSON file and append JSON data into a file in Python. 1. Steps for Appending to a JSON File In Python, appending JSON to a file consists of the following steps: Refer to the following articles to learn how to read JSON from a file and …