Posts

Java 8 Stream - Remove duplicates from a list of objects based on a property

In this section, we will show you how to remove duplicates from a list of objects based on a single property.  In this example, we are getting the stream from the list and putting it in the TreeSet from which we provide a custom comparator that compares id, email, or salary uniquely.   Here we are going to remove duplicates based on the,  id(Long) property of the user  email(String) property of the user  salary(Double) property of the user User.java public class User { private Long id ; private String name ; private String email ; private String phone ; private Double salary ; public Long getId () { return id ; } public void setId ( Long id) { this . id = id; } public String getName () { return name ; } public void setName ( String name) { this . name = name; } public String getEmail () { return email ; } public void setEmail ( String email) { this . email = email; }

Spring boot application.json properties example

Image
In this article, we will show you how to use JSON instead of the properties file or the yml file in Spring Boot by custom configuration. Before moving to the example first you must be familiar with the following components. 1.spring.factories file The file is located in src/java/resources/META-INF/spring.factories . The spring.factories file is a file that spring automatically loads when booting up. It contains references to many configuration classes. For example, org.springframework.boot.env. PropertySourceLoader =com.example.demo. JsonPropertySourceLoader Here we registered our custom Json property source loader 2. YamlPropertySourceLoader Load the resource into one or more property sources. Implementations may either return a list containing a single source or in the case of a multi-document format such as YAML a source for each document in the resource. Refer import org.springframework.boot.env. YamlPropertySourceLoader ; public class JsonPropertySourceLoader ex

Using Spring Interceptor or Using Servlet Filter, how to get a Request URL template that has been hit?

Image
In this section, we will learn how to find a request URL template that has been hit using Servlet Filter or Using Spring Interceptor.  Before going to the example program just check the basis of the Servlet filter and Spring Interceptor. The Servlet Filter is used to intercept the client request and do some pre-processing before they reach the DispatcherServlet. It can also intercept the response and do post-processing before sending to the client in web application. On the other hand, The Spring Interceptor is only applied to requests that are sent to a Controller. Get request URL template using Spring Interceptor(Good approach) Demo Controller @ RequestMapping (path = "/user" ) @ RestController public class UserController { @ GetMapping (path = "/{id}" ) public ResponseEntity < Long > getAllVariables(@ PathVariable ( "id" ) Long id) { return new ResponseEntity < Long >(id, HttpStatus.OK); } } Custom I

How to iterate over Array in Java?Six (6) ways to loop over an Array in Java

Iteration is a technique used to sequence through a block of code perpetually until a concrete condition either subsist or no longer subsists. Iterations are a very prevalent approach utilized with loops. In this article, we will show you  Six (6) ways to loop over an Array in Java. 1.  Using  Simple For loop  2.  Using  Enhanced For loop  3.  Using  While loop 4.  Using  Do - while loop 5. Using Stream.of() and forEach 6. Using Arrays.stream() and forEach Example 1: Using Simple For loop     // create an array String [] users = { "alpha" , "beta" , "giga" , "gama" , "tesla" }; // Simple For loop for ( int i = 0 ; i < users.length; i++) { System.out.println(users[i]); } Example 2:  Using Enhanced For loop  The enhanced for loop is introduced since J2SE 5.0. It is used to traverse the array or collection elements.  // create an array String [] users = { "alpha

Top 50 Hibernate Interview Questions and Answers - Frequently Asked - 2024

Image
1. What is Hibernate? Hibernate is the most popular open-source Java ORM framework in use today. Hibernate allows you to map plain old Java objects to relational database tables. Hibernate also provides data query and retrieval facilities. Hibernate generates the SQL calls and attempts to relieve the developer from manual result set handling and object conversion and keep the application portable to all supported SQL databases.  2. What is ORM? ORM sets the mapping between the set of objects which are written in the preferred programming language like Java and relational database like Oracle. It hides and encapsulates the SQL queries into objects and instead of SQL queries, we can use directly the objects to implement the SQL query. 3. What are the major advantages of Hibernate Framework? Hibernate framework is an open-source framework.  Hibernate framework is a high-performance framework because of its internal cache. Provides facilities to automatically create a table. It provides a