Posts

Showing posts from May, 2020

Spring Boot Starter Dependencies Explained

In this article, I will be covering what Spring Boot starter dependencies are and how they greatly simplify building a Spring application. Edit Problems with dependencies Suppose you are building a Spring application from scratch. Prior to Spring Boot , you had to manually add all the necessary dependencies to your project. So if you use Maven, you had to add all the dependencies to your pom file. For this, you would need to know the group id and artifact id of the dependency. You would also need to know if a particular version of a dependency is compatible with other versions, etc. This often leads to mis-matched dependencies which cause errors in the application. How Spring Boot dependencies address this issue Spring Boot provides starter dependencies. A starter dependency is nothing but a special dependency that aggregates commonly used dependencies for a particular feature. For example, suppose you are building a Spring based web application. For this, you will need to

Hibernate Collection Mapping

In this article, I will be going over how Hibernate Collection Mapping works What is Collection Mapping? Sometimes, you will have a Collection of values associated with an entity. You may want to persist the values in the Collection but may not want to create a separate Entity corresponding to the Collection. You can achieve this via Hibernate/JPA Collection mapping. Hibernate Collection Mapping feature allows you to map any type of Collection like List, Set or Map. How Collection Mapping works JPA provides an annotation called @ElementCollection in order to support collection mapping. You need to specify this annotation on the Collection that you want to persist to the database. Using collection mapping, you can map a collection having primitive type of data as well as non-primitive type (object) data Mapping a Primitive Type In order to map a collection having primitive type of data, you just need to specify the @ElementCollection annotation on the field corresponding to the collec

Java 10 Unmodifiable Collections Changes

One of the new features added by Java 10 is some new methods for creating unmodifiable collections. In this article, I will be covering the Java 10 Unmodifiable Collections. What is an Unmodifiable Collection? As the name implies, an unmodifiable Collection is a Collection that cannot be modified. So if you create an unmodifiable collection and then try to add or remove a value from it, Java throws an Exception What is the use of an Unmodifiable Collection Sometimes, you may need to create a read only copy of a Collection. Unmodifiable collections are useful in such scenarios. How to create an Unmodifiable Collection using Java 10 Java 10 has added a copyOf method to all the Collection interfaces. You can use this to create an unmodifiable Collection. The following code demonstrates this: List<Integer> numberList = Arrays.asList(2,4,6,8,10);numberList.add(12); // Line 1 - no exception List<Integer> unmodifiableList = List.copyOf(numberList);unmodifiableList.add(14); //Li

Java 8 DoubleToIntFunction Example

In this blog post, I will be explaining how the Java 8 functional interface DoubleToIntFunction works. To know more about functional interfaces, you can refer this blog post. Edit What is DoubleToIntFunction The DoubleToIntFunction  interface is a specialization of the  Function  interface. While the  Function interface accepts any data type, the DoubleToIntFunction  interface accepts as parameter a double value and returns a result of type int. To see an example of the Function  interface, refer to  this  blog post.   The DoubleToIntFunction interface provides a method called applyAsInt . It accepts a single parameter of double data type. It returns a result of int data type. So it basically applies the logic in the applyAsInt method to the input parameter and returns the result. DoubleToIntFunction Interface Example Code The following code snippet demonstrates the DoubleToIntFunction interface: DoubleToIntFunction integerRetriever = input -> Double.valueOf(input).intVa

Python Lists With Code Samples and List operations

In my earlier article, I had covered Python numeric and String data types. In this article, I will be covering Python Lists. As the name indicates, you can use a Python list to store a set of values. Creating a Python List You can create a list by specifying comma separated values within square brackets. The following code demonstrates this: mylist = [2,4,'Hello',10.5,"World"]print (mylist) As you can see from the code above, a Python list can contain values of different data types. The above code creates a list mylist that has values of type Integer, decimal and String. Accessing Values in Python Lists You can access the values in a List via the slice operator. It consists of  []  or  [:] . You can access Python lists both in the forward as well as backward direction.  In the forward direction,lists start from position 0 for the first element in the list. In the backward direction, lists start at position -1 for the last value in the list. The following code dem

How to remove white spaces from a String

Very often, you will come across programming scenarios where you will need to remove all white spaces from a String.  In this article, we will see how to remove whitespaces from a String in Java. Using String.trim The String class has a method trim. You can use this to remove leading and trailing white spaces from a String. The following code demonstrates this: String str1 = " Hello World ";System.out.println(str1);System.out.println(str1.trim()); This code creates a String str1 that has leading and trailing whitespaces. It then invokes the trim method. So this code prints the following output:      Hello World Hello World Using String.replace Note that the trim method removes only the leading and trailing white spaces. It does not remove white spaces that are in between the String. There is a replace method on the String class that replaces the specified character with some other character. You can use this method to remove the spaces in a String. The following code demon

Spring Annotation-Based Configuration Example

In this article, I will be demonstrating how to configure a standalone Spring application using annotation-based configuration . I will be using Eclipse and Maven . In order to get a basic introduction to Spring , you can check out this article. In order to understand how to configure a standalone Spring application using XML configuration, you can refer to this article. In order to understand how to configure a standalone Spring application using Java configuration, you can refer to this article. Project Set-up Step 1 – Create a new Maven Project in Eclipse. You can refer to this article on how to create a Maven project in Eclipse. Step 2 – Add Spring dependencies to the maven pom file. You can add the following: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>org