How to sort a list of custom objects using comparable in java / How to implement Comparable in java

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. (more…)

Continue Reading

How to iterate over a list in various ways in java / Different methods of list iteration in java

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!!! (more…)

Continue Reading

Various ways to determine if an entire string is a number in java / How to check if String does not contain number in java

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 a number or not.
Check out: (more…)

Continue Reading

How to create a new Thread in Java / Various methods to create a Thread in Java

A little bit about thread

A thread is a piece of task which is to be executed in parallel to another task executed by an application.
Example, Consider a text editor which performs auto save operation after some particular length of text is typed into it.
Now there are two tasks, first showing the text typed into the editor and second, performing auto save.
Since the two tasks should be executed in parallel, they are performed in different threads. If both operations are done on the same thread, then while the thread is busy saving, it could not perform the typing process and it will show a lag till the save operation completes.

How to create Thread ?

There are a couple of ways of creating a thread. (more…)

Continue Reading

How to do a task repeatedly using threads in java / How to create a timer using ScheduledThreadPoolExecutor in java

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: (more…)

Continue Reading
Close Menu