Monday, 11 December 2017

In this post, we can learn how to count the word occurrences in a given String. In this code, first split the String sentence using space as delimiter . Use any map to store the word as a key and number occurrence as value in the Integer form.

Thursday, 7 December 2017

In the below code, we are passing custom object as key in TreeMap i.e Employee user defined class. In this case, we need to pass the comparator object in the constructor, while creating the TreeMap object. In the Comparator, need to override the compare method and to write the sorting logic.

Tuesday, 5 December 2017

The TreeMap will give the sorting order i.e natural sorting. It will sort the key based on Comparator passed in the constructor while creating the TreeMap.
But If you want to iterate the TreeMap in reverse order then pass Collections.reverseOrder() argument in the constructor of TreeMap.

Signature of reverseOrder() method as,

publicstatic Comparator reverseOrder():

Returns a comparator that imposes the reverse of the natural ordering on
a collection of objects that implement the Comparable interface

Saturday, 2 December 2017

In this post, We can write a simple Spring MVC web application i.e CRUD operation using annotation based Hibernate ORM. The CRUD operation means Add, Edit, Select and Delete. We can create web application using Spring and Hibernate as follows.

First step is to create the Dynamic Web Project in Eclipse , then configure the web.xml as follows,
The project structure i.e folder structure shown in the below image.

In the above configuration, most important is to give the correct spring bean configuration location otherwise it won't take configuration file. In this example, appln-servlet.xml is the bean configuration file kept inside WEB-INF folder.

Hibernate Entity Bean:--

In the example, used annotation based Hibernate not xml configuration. The Annotation based Employee bean is as follows,

Controller class:--The Controller class is a heart of MVC pattern, using @Controller annoation Front controller finds the appropriate controller using HandlerMapping. It will take the request, process it and send model data to the appropriate view.

Spring Bean Configuration (appln-servlet.xml) Next create one xml file under WEB-INF folder, give same file name whatever in the web.xml.This configuration file config the bean and view using some tags. The appln-servlet.xml file is as follows.

View(Jsp page): - I have created two JSP here, addEmployee.jsp and employeeList.jsp. The addEmployee.jsp is using for adding and editing the employee details, employeeList.jsp is for displaying the employee list and delete the particular employee from the list.created under pages folder of WEB-INF,

Running the application using Tomcat Server or any Server:-- If you run the given application without spcifying the URL, it will execute the welcome file i.e add.html so by default add employee will call.