In this post under Java Record, I will explain with example what is custom compact constructor, what is the use and how to add them in Record. In the previous post under Java Record, I showed what is canonical constructor, what is custom canonical constructor and what is the purpose of it. Below are the…… Continue reading Custom Compact constructor in Record
Category: Java
Text Block With Indentation
In this post under Java Core, I will show you with example how indentation is calculated in Text Block. Below is the main class with three variety of text block. Example3 package core.string; public class Example3 { public static void main(String[] args) { String textBlock1 = """ <html> <head> <title>TextBlock1<title> </head> <body>Body of TextBlock1</body> </html>…… Continue reading Text Block With Indentation
Text Block simple example
In this post under Java Core, I will introduce you to newly added “Text Block” feature. This feature was added as part of Java 15. Pre Java 15, if we have to declare a multiline string we used to declare it as shown below String result = "Since Java 15, text blocks are \n" +…… Continue reading Text Block simple example
Using Comparable with multiple fields
In this post under Java Collections, I will show an example of how to use Comparable interface when we need to sort collections on multiple fields For our example, we will sort a collections of employee objects. I need to sort the collections of employee objects in ascending order first using there name and then…… Continue reading Using Comparable with multiple fields
Pattern matching with instanceof
Hi all, in this post under Java pattern matching section, I will introduce you to pattern matching and how to use it with instanceof operator. Please don’t confuse java pattern matching with regex pattern matching Regex pattern matching checks whether a given text matches a text pattern and may optionally extract parts of the text.…… Continue reading Pattern matching with instanceof
List Fill demo
In this post under Java Collections, I will explain with example the purpose of “fill” static method in “Collections” utility class. This method takes any list as input and replace all the elements of the list with user given object. So if we have a list named “entries” containing below elements [1, 2, 3, 4,…… Continue reading List Fill demo
List nCopies demo
In this post under Java Collections, I will explain with example the purpose of “nCopies” static method in “Collections” utility class This method creates a list containing n copies of input object. Where n and input object are user specified. So basically this method takes two arguments.1) The object to be replicated in the list2)…… Continue reading List nCopies demo
List rotation demo
In this post under Java Collections, I will explain with example how to rotate elements in a list. Java “Collections” utility class has static method named “rotate” which will rotate elements in a list. This method takes two arguments.1) the list2) integer representing the distance Below is the complete main class for your reference Main…… Continue reading List rotation demo
Populating the entire array with different elements
In this post under Java Collections, I will show with example an api provided in “Arrays” class that will populate an array with different element. We are talking about “Arrays” class static method “setAll”. It takes two arguments1) the array to be populated2) a function that will generate the elements that are to be inserted…… Continue reading Populating the entire array with different elements
Populating the entire array with same element
In this post under Java Collections, I will show with example an api provided in “Arrays” class that will populate any array with a particular element. We are talking about “Arrays” class static method “fill”. It takes two arguments1) the array to be populated2) the element to be fill int array For our example I…… Continue reading Populating the entire array with same element