Overall notion of Singleton class lies in getting one and only one instance of the class. Much of our functionality relies on the base that there will be only one object of a class we intentionally designed to be Singleton. What if the same class produces more than one object in some scenario? Woaaah!!! Our code blows away.Read More →

Sort list of objects on a field Suppose you have a list which contains objects of your own class rather than java’s built in classes such as java.lang.Integer, java.lang.String etc. Now you want this list to be sorted on a particular field of this class. Let’s say we have a User class which contains user details such as name, age, address etc., and we have a list of various users. Now we want a list wherein users are listed in the order of their increasing age. That is, we need to sort the above list in the order of age. Collections.sort method You can sortRead More →

Iterating over a collection is a task that most developers are required to perform during their development task and most of us keep on using the same way which we have used earlier though there are many ways to do this. In this post I am going to list down all those ways. We all might be familiar with these ways. Still, keep on reading to refresh!!! First let’s initialize a list List list = Arrays.asList(new String[]{ “first”,”second”,”third”,”fourth”, “fifth” }); Now let’s begin iteration: Method 1: Using for loop for (int count = 0; count < list.size(); count++) { System.out.println("Item:: " + list.get(count)); } DetailRead More →

There may be a scenario, if we want to determine whether a string is completely numeric or not, such as we want a user to input a number and our program should multiply it by 5 and return the result. User is a human. He may mistakenly input an alphanumeric string by mistake or to check the correctness of our program. Off course, we would not like to show an exception trace to the user instead of the result. Better we check the input for its validity before processing. Below are various ways using which we may check that an input String is completely aRead More →

Suppose we want to do a task after fixed time interval such as creating a beep sound after 5 seconds or checking the connection status after every 5 seconds. This may be easily accomplished using Executor framework in java. There is a class called ScheduledThreadPoolExecutor which has the methods to perform such kind of scheduling. Let’s get down to code right away:Read More →

Loading a class more than once using java’s built in ClassLoader is not possible because: Builtin ClassLoader always checks if a class has already been loaded. If it is, it simply does nothing and returns. Hence to reload a class, you have to use your custom ClassLoader. Code to reload a class using custom ClassLoader is as follows:Read More →