Sunday, March 27, 2016

How to increase length of existing VARCHAR column in SQL Server

You can increase the length of a VARCHAR column without losing existing data in SQL Server. All you need to do is execute following ALTER TABLE statements. Though, you need to specify NULL or NOT NULL constraint explicitly, depending upon your data.

Here is the SQL command you can use to increase the length of a VARCHAR column in SQL Server:


This command increases the length of the title column of the Books table to 432 characters. You can use the same command to increase the length of CHAR, NCHAR, or NVARCHAR columns as well.

Saturday, March 26, 2016

How to find the first element of Stream in Java 8 - findFirst() Example

In Java 8, you can use the Stream.findFirst() method to get the first element of Stream in Java. This is a terminal operation and often used after applying several intermediate operations e.g. filter, mapping, flattening, etc. For example, if you have a List of String and you want to find the first String whose length is greater than 10, you can use the findFirst() method along with stream() and filter() to get that String. The stream() method gets the Stream from a List, which then allows you to apply several useful methods defined in the java.util.Stream class e.g. filter(), map(), flatMap() etc.

Friday, March 18, 2016

How to Reverse an ArrayList in Java using Recursion - Example Tutorial

If you ever need to reverse a List in Java e.g. ArrayList or LinkedList, you should always use the Collections.reverse() method. It's safe and tested and probably performs better than the first version of the method you write to reverse an ArrayList in Java. It's also one of the recommended best practice to prefer library method instead of writing your own, as advised by great Joshua Bloch in Effective Java. There are so many benefits of using library methods e.g it was written by experts, thoroughly and open-source tested, and more actively maintained. Anyway, if you have been asked to write a function to reverse a List using recursion in Java, maybe as part of your homework, assignment, or during an interview, then you are in the right place.

Saturday, March 12, 2016

How to Reverse a String in place in Java - Example

It's possible to reverse a String in place by using a StringBuilder. Since String is Immutable in Java, it's not possible to reverse the same String, but you can minimize the number of intermediate String objects by using StringBuilder or StringBuffer, which are mutable. The algorithm to reverse the String in place is similar to the algorithm we have used earlier to reverse an array in place. You need to traverse the String from one end, swapping characters at another end until you reach the middle of the String. At the point characters in your String is reversed. This algorithm only requires one extra character of memory to facilitate the swapping of characters. The time complexity of this algorithm is O(n/2) i.e. O(n) where n is the length of String.

Sunday, March 6, 2016

How to convert an Array to HashSet in Java - Example Tutorial

The array was there in Java before the Collection framework makes its entry in JDK 1.4, hence, you will find several legacy codes, which accepts array than the Collection or Set. Due to this, we often need to convert an array to different types of collection e.g. List or Set. Earlier, I have told you the trick to convert an array to list and today I'll teach you how to convert an array to HashSet by using the same technique. The concept you need to know is that Collection provides a copy constructor i.e. you can pass a collection to create another collection. We'll leverage the same fact and to create a HashSet, we'll pass an ArrayList.