Search results for: Java

Multiple data source implementations are very crucial in instances where we want to secure the application from vulnerabilities such as a database failure. This will ensure that the application can still function even if part of the database went down. This tutorial will use student and course entities to demonstrate how multiple data sources can…

Read More Multiple Datasources in Spring Application

This article is the third of a series on Reactive Programming. If you have not read the previous article, I would strongly advise you to do so. In this article, we will discuss how Reactive Programming is made possible in Spring. Spring WebFlux Spring WebFlux is the Spring project that makes Reactive Programming possible in…

Read More Reactive Programming In Spring Framework

This tutorial demonstrates how to deploy a Spring Boot REST app to Tomcat 10. Apache states that the Jakarta EE platform represents the evolution of the Java EE platform. Tomcat 10 and later versions implement specifications that were developed as part of Jakarta EE. In contrast, Tomcat 9 and earlier versions implement specifications that were…

Read More Deploy a Spring Boot App as a WAR to Tomcat 10

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

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

This tutorial will teach you how to create your own Spring Cloud Config Server and how to configure a Spring Boot Application to be a Spring Cloud Config Client. Spring Cloud Config Server Spring Cloud Config Server is a Spring Boot application. So to create our own Spring Cloud Config Server, we must first create…

Read More Spring Cloud Config Server and Config Client

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

We have the isPresent() and ifPresent() methods to help us detect if Optional contains some value or not. Optional isPresent() method The isPresent() method returns true if there is a value present in the Optional, otherwise false.Using it, we can avoid getting an exception when trying to get the value from an empty Optional. So…

Read More Optional – ifPresent() and isPresent() methods

public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier) class Test { public static void main(String[] args) { List<String> cities = new ArrayList<>(Arrays.asList(“Paris”, “Bern”, “London”, “Tokyo”, “Boston”)); Map<Integer, List<String>> resultMap = cities.stream() .collect(Collectors.groupingBy(String::length)); System.out.println(resultMap); } } Output: {4=[Bern], 5=[Paris, Tokyo], 6=[London, Boston]} class Test { public static void main(String[]…

Read More Streams – groupingBy() operation

class Test { public static void main(String[] args) { List<String> words = new ArrayList<>(Arrays.asList(“Hello”, “World”, “of”, “Java!”)); String result = words.stream().collect(Collectors.joining()); System.out.println(result); String resultWithDelimiters = words.stream().collect(Collectors.joining(“-“)); // delimiter System.out.println(resultWithDelimiters); String resultWithDelimitersAndPrefixAndSuffix = words.stream().collect(Collectors.joining(“-“, “(“, “)”)); // delimiter, prefix, suffix System.out.println(resultWithDelimitersAndPrefixAndSuffix); } } Output: HelloWorldofJava! Hello-World-of-Java! (Hello-World-of-Java!) class Test { public static void main(String[] args) {…

Read More Streams – collect() operation