devxlogo

Java

How to Annotate Class with Lombok

The first approach consists of annotating the class as below: @ToString@EqualsAndHashCode@Getter@Setter@RequiredArgsConstructorpublic class Foo { …} A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, @Setter on all non-final fields, and

Understanding the Unary Operator in Java

Java uses the symbol tilde (~) to represent the operator. The following examples are clearly illustrative of the use and outcome of this operator. Code sample: public class JavaUnary{public static

Validate Field Length

See how to validate the length of a field in Java via javax.validation.constraints.Size. We can do it as follows: @NotNull@Size(min=3, message=”Name must be at least 3 characters long”)private String name;

Using the FileTime Class in Java

FileTime is a class in Java that helps you retrieve time-related information regarding files. One such method that we will see below is to get the modified time of a

Working with Long Integer Literals

Literals of type long are expressed by adding an L suffix. For example: 0L // The decimal number zero (type ‘long’)1L // The decimal number one (type ‘long’)2147483648L // The value

Set Hibernate Batching Size

Setting the Hibernate batching size can be accomplished via hibernate.jdbc.batch_size. In Spring, we need to add in application properties per the following setting (the recommended batch size is between 5

Creating an Immutable List via JDK 9

Starting with JDK 9, we can create an immutable list via the suite of List#of(…) methods. For example, an immutable list of 3 elements can be created as follows: import java.util.List;…List

Repeating Text n Times via Stream.generate()

There are several approaches for repeating a string n times. In functional-style, we can rely on Stream.generate(), as in the following example: String result = Stream.generate(() – TEXT) .limit(5) .collect((joining()));