devxlogo

Java

How to Convert a List to an Array

One way to convert a list to an array is shown below: List list = Arrays.asList(1, 2, 3);Integer[] array = new Integer[list.size()];array = list.toArray(array);

How to Trigger an Async Query

Query methods defined in repositories can be executed asynchronously. This means that the query method will return immediately upon invocation. The sample code below relies on @Async annotation and Future,

How to Selectively Expose CRUD Operations

In order to selectively expose CRUD operations, we need to define an intermediate interface, annotated as below: @NoRepositoryBeaninterface IntermediateRepository extends Repository { // add here the selected CRUD, for example

How to Quickly Sort an Array

The quickest solution for sorting an array in Java relies on Arrays.sort() method as below: int[] arr = {5, 12, 3, 44, 55}; Arrays.sort(arr);

Allow Only One Null Property

Consider a Review object with three properties: article, book and magazine. Let’s assume that only one of these three properties should be set as non-null. For checking this constraint we

How to Declare a Pattern in Java

The best way to declare a Pattern in Java is as a constant, since Pattern is immutable. Use the following code: private static final Pattern PATTERN = Pattern.compile(” +”); Further,