Java

In this tutorial, you will learn how to use specification and predicate in Spring Data JPA using the Spring Boot RESTful API project. Spring Data JPA Specifications allow us to create dynamic database queries by using the JPA Criteria API. It defines a specification as a predicate over an entity. Spring has a wrapper around…

Read More Specification & Predicate: Advance Search and Filtering in JPA

This tutorial will be part of our Series on Reactive Programming in Java and Spring Framework. If you have not read the previous article, I would strongly advise you to do so. This will help you to understand better as we go further. In the previous blog post, we discussed publishers and subscribers in Java.…

Read More Reactive Programming: Creating Publishers and Subscribers in Java.

public sealed class Vehicle permits Car, Truck, Bus {} final class Bus extends Vehicle { } non-sealed class Truck extends Account { } sealed class Car permits BlueCar, RedCar{ } public abstract sealed class User permits PremiumUser, AdvancedUser { public abstract String getUserData(); } non-sealed class PremiumUser extends User { @Override public String getUserData() {…

Read More Sealed Classes and Interfaces in Java

class Test { public static void main(String[] args) { String name = null; System.out.println(name.toUpperCase()); } } Output: Exception in thread “main” java.lang.NullPointerException at com.example.Test.main (Test.java:9)   Here, we got an error message that the NPE has occurred on line 9. Unfortunately, we can’t conclude which value exactly was null from the message. class Test { public static…

Read More Helpful NullPointerExceptions (NPE) in Java

class User { private String name; private String username; private String membershipType; private String address; public User(String name, String username, String membershipType, String address) { this.name = name; this.username = username; this.membershipType = membershipType; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public…

Read More Java Records

String multilineText= “”” Hi, this is one simple example of a multiline text”””; class Test { public static void main(String[] args) { String example = “{\”key1\”:\”value1\”,\”key2\”:” + “\”value2\”,\”key3\”:\”value3\”}”; System.out.println(example); } } Output: {“key1″:”value1″,”key2″:”value2″,”key3″:”value3”}   Now, let’s use a Text Block: class Test { public static void main(String[] args) { String example = “”” { “key1”:…

Read More Java Text Blocks

private static void sumWithRegularStream() { for (int i = 0; i < 10; i++) { IntStream.rangeClosed(1, 100000).sum(); } } private static void sumWithParallelStream() { for (int i = 0; i < 10; i++) { IntStream.rangeClosed(1, 100000).parallel().sum(); } } class Test { public static void main(String[] args) { long regularStreamStartTime = System.currentTimeMillis(); sumWithRegularStream(); System.out.println(“Regular Stream execution…

Read More Parallel Streams Performance Testing

class Test { public static void main(String[] args) { System.out.println(“Normal Stream…”); IntStream integers = IntStream.rangeClosed(1, 10); integers.forEach(num -> System.out.print(num + ” “)); System.out.println(); System.out.println(“Parallel Stream…”); IntStream integers2 = IntStream.rangeClosed(1, 10); integers2.parallel().forEach(num -> System.out.print(num + ” “)); } } Output: Normal Stream… 1 2 3 4 5 6 7 8 9 10 Parallel Stream… 7 6…

Read More Introduction to Parallel Streams API in Java

class Test { public static void main(String[] args) { Optional<String> stringOptional = Optional.ofNullable(“value1”); stringOptional.map(value -> value.concat(“234”)) .ifPresent(System.out::println); } } Output: value1234 2. Using the flatMap() method class Student { private String firstName; private String lastName; private int grade; private Optional<String> address; public Student(String firstName, String lastName, int grade) { this.firstName = firstName; this.lastName = lastName;…

Read More Optional – map() and flatMap() operations

public Optional<T> filter(Predicale<T> predicate) class Test { public static void main(String[] args) { Optional<String> stringOptional = Optional.ofNullable(“alegru coding”); stringOptional.filter(str -> str.length() > 10) .map(String::toUpperCase) .ifPresent(System.out::println); } } Output: ALEGRU CODING class Test { public static void main(String[] args) { Optional<User> userOptional = Optional.ofNullable(new User(“John”, “john123”, “premium”, “5th Avenue”)); userOptional.filter(user -> user.getMembershipType().equals(“premium”)) .ifPresent(System.out::println); } } class…

Read More Optional – filter() operation

In this lesson, we will cover the three useful methods from the Optional class: public T orElse(T other) – It returns the value if present, otherwise returns the other. public T orElseGet(Supplier<? extends T> other) – It returns the value if present. Otherwise, invoke other and return the result of that invocation. public <X extends…

Read More Optional – orElse(), orElseGet() and orElseThrow() methods