Download an Excel using Servlet

How to download an Excel using Servlet? Create a workbook using any frameworks and write out the created workbook to response.getoutputstream(). For creating the excelsheet I am using apache poi. Set the content type of response to “application/vnd.ms-excel” and set response header as   “Content-Disposition”, “attachment; filename=YourExcelName.xls”. Download an Excel using Servlet Example   Discussion Should I close HttpServletResponse.getOutputStream()? You … Read more

Flatten a list of Strings

In this tutorial we would see at various ways to concatenate a list of strings and insert a separator “,”  in between the concatenated strings. Java 8 output Using org.apache.commons.lang3.StringUtils output : Using loop See concatenation comparison for choosing a better way for string concatenation. output

Runtime comparison of string concatenation

One of the mostly asked questions is “How should I do string concatenation?”. And the answer to this question is not very straight forward.  Here I would benchmark the three string concatenation operations. Tools Used I am using jmh to do the benchmarking. And used maven to run the benchmark code  on java 8 as recommended … Read more

Convert List to Array in Java

In this tutorial we will see how to convert a list of objects to an Array.  That can be done by toArray method on the list. We are going to use the below two methods to do the conversion. Both these methods preserve order of the original list being converted. We would also discuss the conversion of … Read more

Mergesort in java

Mergesort is a divide and conquer algorithm. It works by breaking an array into sub-arrays and then recursively sorting the sub-arrays. Steps for Mergesort Divide the list with n elements into n sublists with each sublist having 1 element. We will divide list into 2 sublists by dividing in the middle and then recursively dividing … Read more

How to take input from user in java

The simplest and the easiest way to read the user input in java is by using the Java scanner class. What is scanner class? java.util.Scanner is a simple text scanner that can parse primitive types and strings using regular expressions. The input to Scanner class is broken into tokens using a delimiter pattern, which by … Read more

Autoboxing and its pitfalls

Autoboxing What is Autoboxing? Autoboxing is a feature of Java language introduced in Java 1.5.  When Java compiler makes automatic conversion between the primitive types and their corresponding object wrapper class, it is called autoboxing.  The process of creating a Wrapper class like Float from a primitive type like float is called boxing.  And the process of creating … Read more

Java interview questions, coding in a team

When a programmer joins a team, he is at least expected to have a basic understanding of the process of handling  code within a team. And the following questions have been taken from real world interviews and groups discussions Q. How can a team work on a code that is used by a team of … Read more

Stop eclipse formatter putting all enums in one line

While working with eclipse you would have experienced that if one tries to put enums on different lines and by chance someone formats the code using eclipse formatter( ctrl+shift+f), all the manual formatting is removed. Of course you can undo it. But what if someone else working on your code removes your manual formatting and adds … Read more

Eclipse, svn folder properties

Changes in properties of a folder. When you are adding a file to svn ignore list then you need to commit the top folder. Because you have changed the properties of the folder. svn:ignore list is not a local list. I always thought so. The svn ignore list is in the properties of that folder that exists in … Read more