Posts

Showing posts from June, 2019

Java 8 LocalDate Class explained with code samples

Another new feature by Java 8 is the DateTime API. Some of the important classes in the DateTime API are the LocalDate, LocalTime and LocalDateTime . In this article, I will be explaining the Java 8 LocalDate class in detail. Edit Introduction The LocalDate class represents a date. It is present in the java.time package. It has several methods that allow you to perform date manipulation easily. Creating LocalDate There are several static methods on the LocalDate class that allow you to obtain a LocalDate instance. The following code demonstrates this: public class LocalDateDemo { public static void main(String[] args) { LocalDate date1 = LocalDate.now(); System.out.println("Today's date is "+date1); LocalDate date2 = LocalDate.parse("2019-07-15"); System.out.println("Date2 is "+date2); LocalDate date3 = LocalDate.of(2017,05,17); System.out.println("Date3 is "+date3 ); LocalDate date4 = LocalDate.ofYear

Java 8 DoubleConsumer example

In this blog post, I will be explaining how the Java 8 functional interface DoubleConsumer works. To know more about functional interfaces, you can refer this blog post. Edit The DoubleConsumer interface provides a method called accept. It accepts a single parameter of double data type. It does not return anything, it returns a void. So it operates via side effects i.e. it modifies the parameter passed in. The  DoubleConsumer  interface is a specialization of the Consumer interface. While the Consumer interface accepts any data type, the DoubleConsumer interface accepts a Double value. To see an example of the Consumer interface, refer to  this  blog post. DoubleConsumer Example Consider the following code snippet: public class DoubleConsumerDemo { public static void main(String args[]) { DoubleConsumer squareGenerator = (num) -> System.out.println(num*num); double input = 5.0; squareGenerator.accept(input); }}   Here, the DoubleConsumer.accept method accepts a dou

Hibernate Association Mappings with code samples

Hibernate supports associations that help you to model the relationship between database tables. In this article, I will be covering Hibernate association mappings. Introduction Most real-world applications have tables that have dependencies on each other i.e. the primary key of a table will be the foreign key in another table. Hibernate associations help us to represent the relationships between the tables. Types of association mappings Hibernate supports 4 types of associations as follows One to One You can use a One To One association mapping to represent a one-to-one relationship between database tables. A one-to-one relationship occurs between tables when one record from the main table corresponds to only one record in the second table. For example, a Person can have only one address. So one record in a Person table will correspond to only one record in the address table. One To Many You can use a One To Many association mapping to represent a one to many relationships between ta

Creating a Spring REST application in Maven and Eclipse

Image
In this article, I will be demonstrating how to create a REST application via Spring using Maven and Eclipse. Create a Project and And Maven Support Step 1 – Follow the steps in this article to create a web project in Eclipse with Maven Support. You should see a project as follows: Edit Step 2 – Add the Maven Dependencies for Spring to the pom file as follows: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.1.3.RELEASE</version> </dep

Stream API Filter examples with code samples

In this blog post, I will be demonstrating the filter method provided by Java 8 Stream API.In order to understand the Stream API in detail, refer to  this  blog post. Edit Introduction The filter method in the Stream API can be applied on an existing Stream. It creates a new Stream with all the elements that match the condition specified in the filter method. It accepts a Predicate instance which is an in-built functional interface. Refer  this  blog post for a detailed  Predicate  example. Code Sample with Integer public class FilterStreamDemo { public static void main(String[] args) { List<Integer> input = Arrays.asList(5, 3, 11, 15, 9, 2, 5, 11); Stream<Integer> stream = input.stream().filter(num -> num >= 9); stream.forEach(num -> System.out.println(num)); }} This code uses the filter method to obtain all the elements that are greater than 9 .  It first obtains a stream instance on the input ArrayList using the  stream() method. Then the

How to create a Maven web project in Eclipse

Image
In this blog post, I will be explaining how to create a web project in Eclipse with Maven support. Create a Dynamic web project Step 1 -Click on “File –> New –> Other” Step 2 – Click on Dynamic Web Project Step 3 – Type some project name Step 4 – Click on Next Step 5 – Click on “Next” Step 6 – “Click on Finish”. This will create a project as follows: Create web.xml Sometimes, Eclipse may not automatically create the web.xml file. So if this file is not present in your web-inf folder, you need to follow these steps. Step 1 – Right click on project –> Java JEE Tools –> Generate Deployment Descriptor Stub This will create a web.xml file as follows: Add Maven Support Step 1 – Right-click on Project –> Configure –> Convert to Maven Project Step 2 – Click Finish This will add a pom file to the project as follows: Now you can add the necessary dependencies to your pom file. So for example, if you are creating a Spring application, you can add Spring dependencies. You can als

Controller Vs RestController annotation in Spring

In this blog post, I will be explaining the Controller and RestController Spring annotations and also their differences. MVC V/s REST Before diving into the Controller and RestController annotations, it is important to understand the difference between a normal MVC web application and a REST application. An MVC Web application returns HTML which is rendered in a browser and displayed to end users.  A REST application, on the other hand, returns an XML or JSON response. Edit Controller Annotation You can use the Controller annotation to designate a Controller class in an MVC application. Normally in such scenarios, the code maps each method in the controller to a URL specified via the RequestMapping annotation. When the UI requests a particular URL, the code invokes the appropriate method. This executes the business logic and then redirects the user to the appropriate JSP page. Consider the following code: @Controllerpublic class HelloWorldController { @RequestMapping("

Java 8 BiFunction Example

In this blog post, I will be explaining how the Java 8 functional interface BiFunction works. To know more about functional interfaces, you can refer  this  blog post. Edit The BiFunction is a specialization of the Function interface that accepts 2 arguments. Just like Function it provides a method called apply. This method accepts 2 arguments of any data type and returns a result. So it basically applies the logic in the apply method to the input parameters and returns the result. BiFunction Interface Example with two integer inputs and Integer output Consider the following code snippet: private static void example1(){ BiFunction<Integer,Integer,Integer> multiplier = (a,b) -> {return a*b;}; int input1 = 6; int input2 = 8; System.out.println("Result of multiplying "+input1+" and "+input2 +" is "+multiplier.apply(input1,input2) ); } Here, we have written a BiFunction implementation that accepts 2 integer values, multiplies them and ret

Java Arrays explained with code samples

Introduction An array is essentially a list of values referred to by a common name. Java arrays can be one-dimensional or multi-dimensional. Declaring and Initializing an Array Consider the following code snippet: int myArray[];myArray = new int[10];   Here, int refers to the type of data that will be stored in the array and myArray is the name given to the array. In the second line myArray is allocated memory. The new keyword is used.  [10] – indicates the size of the array. So this statement creates a new array with space for 10 integer values. The declaration and memory allocation can also be combined into a single statement as follows: int myArray[] = new int[10];   Accessing the elements of an array Each value within the array is called an element in the array. You can access any element in an array by specifying its position within square brackets . The array index begins at 0, so the 1 st element is at position 0, the 2 nd at position 1 and so on. Consider the following co

How to create HelloWorld Spring MVC Web application using Maven and Eclipse

Image
In this blog post, I will be explaining how you can create a Spring MVC web application. In order to learn the Spring framework from scratch, you can enroll in this Spring Course . Create a Project and And Maven Support Step 1 – Follow the steps in this article to create a web project in Eclipse with Maven Support. You should see a project as follows: Edit Step 2 – Add the Maven Dependencies for Spring to the pom file as follows: <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version&g