Tuesday, September 23, 2014

Insertion Sort in Java with Example

Insertion sort is next simple sorting algorithm after Bubble Sort. You may not have realized but you must have used insertion sort in lot of places in your life. One of the best example of insertion sort is, how you sort your hand in playing cards. We pick one card from deck, we assume it's sorted, and then we insert subsequent card in their proper position. For example, if our first card is Jack, and our next card Queen then we put that after Jack. Now if next card is King, we put it after queen, and if we get 9, we put it before jack. So if you look closely, insertion sort is perfect sorting algorithm to insert new value in already sorted list. That's why best case complexity of insertion sort is O(n), in which case you insert a new number in already sorted list of integers. Another thing to keep in mind is size of list, insertion sort is very good for small list or array, but not so for large list, where QuickSort, MergeSort and HeapSort rules. Let's see one more example of insertion sort from real life. Have you noticed, how does tailors arrange shirts in wardrobe, according to size. So they insert new shirt at proper position, for that they shift existing shirts, until they find the proper place. If you consider wardrobe as array and shirts as element, you will find out that we need to shift existing elements to find right place for new element. This is the core of insertion sort algorithm, if you understand these example, even you can come up with a step by step coding algorithm to sort an array of integer using insertion sort in Java. In this article, we will learn that by first understanding insertion sort with flowchart and by walking through an example. After than writing a Java method to do insertion sort will be very easy.

Thursday, September 18, 2014

How to Convert Array to String in Java with Example

Array and String are very closely related, not with anything else but with popularity. Many times we need to convert an array to String or create a array from String, but unfortunately there is no direct way of doing this in Java. Though you can convert array to String by simply calling their toString() method, you will not get any meaningful value.  If you convert an integer array to String, you will get something like I@4fee225 due to default implementation of toString() method form java.lang.Object class. First I shows the type of the array and content after @ is hash code value in hexadecimal. How valuable that is? This is not what I want to see, I was interested on contents rather than hashcode. Fortunately Java provides a utility class called java.util.Arrays, which provides several utility method for arrays in Java. For example, here we have method to sort array, search elements using binary search, fill array, methods to check if two arrays are equal or not, copy range of values from one array to another,  and much needed toString() and deepToString() method to convert both one dimensional and multi-dimensional array to String. These method provides content view of array, for example, when you convert an integer array {1, 2, 3, 4, 5, 6, 7} to String, you will get [1, 2, 3, 4, 5, 6, 7] instead of  [I@2ab6994f . In this article, we will see examples to convert different types of array to String e.g. int, char, byte, double, float, Object and String array itself.  We will also learn how to convert two dimensional array to String in Java.

Tuesday, September 16, 2014

How to Read, Write XLSX File in Java - Apach POI Example

No matter how Microsoft is doing in comparison with Google, Microsoft Office is still the most used application in software world. Other alternatives like OpenOffice and LiberOffice have failed to take off to challenge MS Office. What this mean to a Java application developer? Because of huge popularity of MS office products you often need to support Microsoft office format such as word, Excel, PowerPoint and additionally Adobe PDF. If you are using JSP Servlet, display tag library automatically provides Excel, Word and PDF support. Since JDK doesn't provide direct API to read and write Microsoft Excel and Word document, you have to rely on third party library to do your job. Fortunately there are couple of open source library exists to read and write Microsoft Office XLS and XLSX file format, Apache POI is the best one. It is widely used, has strong community support and it is feature rich. You can find lot of examples of how to do with Excel using Apache POI online, which means you will never feel alone and has instant Google support if you stuck there. In this article, we will learn how to read and write excel files in Java. As I said, Excel files has two popular format .XLS (produced by Microsoft Officer version prior to 2007 e.g. MS Office 2000 and 2003) and .XLSX (created by Microsoft Office 2007 onwards e.g. MS Office 2010 and 2013). Fortunately Apache POI supports both format, and you can easily create, read, write and update Excel files using this library. It uses terms like workbook, worksheet, cell, row to keep itself aligned with Microsoft Excel and that's why it is very easy to use. Apache POI also provides different implementation classes to handle both XLS and XLSX file format.

Java67 Headline Animator