Wednesday, December 17, 2014

How to break from nested loop in Java

There are situations we need nested loops in Java, one loop containing another loop e.g. to implement many O(n^2) or quadratic algorithms e.g. bubble sort, insertion sort, selection sort, and searching in two dimensional array. There are couple of more situations where you need nested looping e.g.  printing pascal triangle and printing those star structures exercises from school days. Sometime depending upon some condition we also like to came out of both inner and outer loop. For example while searching a number in a two dimensional array, once you find the number, you want to come out of both loop. Question is how can you break from nested loop in Java. You all know about break right? you have seen break in switch statements, or terminating for, while and do while loop, but not many Java developer know but there is a feature called labeled break, which you can used to break from nested loop. All the places, where you have used break before is example of unlabeled break, but once you use label with break you can terminate a particular loop in nested loop structure. In order to use labeled for loop, you first need to label each loop as OUTER or INNER, or whatever you want to call them. Then depending upon from which loop you want to exit, you can call break statement as shown in our example. By the way, there is a better way to do the same thing, by externalizing the code of nested loop into a method and using return statement for coming out of the loop. This improves readability of your algorithm by giving appropriate name to your logic.

Thursday, December 11, 2014

Strategy Pattern in Java with Example

Can you tell me any design pattern which you have used recently in your project, except Singleton? This is one of the popular question from various Java interviews in recent years. I think, this actually motivated many Java programmers to explore more design patterns and actually look at original 23 patterns introduced by GOF. Strategy pattern is one of the useful pattern you can mention while answering such question. It's very popular and there are lots of real world scenario where Strategy pattern is very handy. Many programmer ask me what is the best way to learn design pattern, I say you first need to find how other people use it and for that you need to look at the open source libraries you use in your daily task. JDK API is one such library I use on daily basis and that's why when I explore new algorithms, design pattern, I first search JDK for their usage. Strategy has found its place in JDK, and you know what I mean if you have sorted ArrayList in Java. Yes, combination of Comparator, Comparable and Collections.sort() method are one of the best real world example of Strategy design pattern. To understand it more, let's first find out what is Strategy pattern? First clue is in the name itself.  The strategy pattern defines a family of related algorithms e.g. sorting algorithms like bubble sort, quicksort, insertion sort  and merge sort, or compression algorithm e.g. zip, gzip, tar, jar, encryption algorithm e.g. MD 5, AES etc and lets the algorithm vary independently from clients that use it. For example, you can use Strategy pattern to implement a method which sort numbers and allows client to choose any sorting algorithm at run time, without modifying client's code. So essentially Strategy pattern provides flexibility, extensible and choice. You should consider using this pattern when you need to select an algorithm at runtime. In Java, strategy is usually implemented by creating a hierarchy of classes that extend from a base interface known as Strategy. In this tutorial, we will learn some interesting things about Strategy pattern by writing a Java program and demonstrating how it add value to your code.

Tuesday, December 9, 2014

2 Ways to Print Custom String Value of Java Enum

We all know that how powerful enumeration type in Java is, and one of the main strength of enum is that they can implement interface, they can have a instance variable and you can also override any method inside enum instance. In Java programs, we often need  need to convert Enum to String type, sometime just to print values in log file and other time for storing log into database.  By default, when you print an enum constant, it print its literal value e.g. if name of enum instance is RED, then it will print RED. This is also the value which is returned by name() method of java.lang.Enum class. But, there are situations when we want a custom String value for enum constant. For example, I want to print Red instead of RED when I convert Enum to String. How do you do that? Well, there are two ways you can achieve this, first by overriding toString() method for each enum constant and second by using an instance variable to hold custom String value. You can provide custom value while creating Enum constants and later you can call that method which returns custom  String value. In this article, we will see example of these two ways.

Java67 Headline Animator