In this Java tutorial, I will teach you how to sort an ArrayList on both increasing and decreasing order of elements. Though, we will use ArrayList of Integers on examples, you can practically sort any list by using this technique. Only requirement is that your object should implement Comparable interface otherwise you need to pass Comparator implementation to sort objects stored inside list. The sort() method is defined inside java.util.Collections class. It is overloaded to accept a Comparator. The version which doesn't take any argument sort the given list on default order e.g. increasing order for integers. This is defined by their compareTo() method which comes from Comparable interface. The version which takes Comparator, uses that to sort the list, as we have done to sort the list in reverse or decreasing order.
Saturday, 31 October 2015
Saturday, 26 September 2015
How to Convert String to Date in Java in yyyy-MM-dd format
In this tutorial, I will show you how to convert String to Date in Java which is in yyyy-mm-dd format. We will use DateFormat and SimpleDateFormat class from java.util.text package for this conversion.
Here are the steps :
1) Create a DateFormat with format String as "yyyy-MM-dd" (remember, small "m" is for minutes, and capital "M" is for month)
2) Call the parse() method with given String, this will return a java.util.Date object
Here are the steps :
1) Create a DateFormat with format String as "yyyy-MM-dd" (remember, small "m" is for minutes, and capital "M" is for month)
2) Call the parse() method with given String, this will return a java.util.Date object
Saturday, 19 September 2015
Singleton Design Pattern using Double Checked Locking Idiom
In my last interview, I was asked to write code to implement Singleton design pattern using "Double checked locking" idiom, which I thought to share with you all. In Singleton design pattern, only instance of class is created during application lifetime and that's shared between all its clients. One of the challenge with Singleton pattern is to ensure that only instance of class is created without compromising performance.
How to convert String to int and Integer in Java? An Example
There are two main way to convert a String to int primitive and Integer class in Java :
1) Integer.parseInt(), which takes a String and return an int primitive
2) Integer.valueOf() ,which accept a String and return an Integer object
1) Integer.parseInt(), which takes a String and return an int primitive
2) Integer.valueOf() ,which accept a String and return an Integer object
Sunday, 6 September 2015
Difference between Vector and ArrayList in Java
One of the classic question from from Java interviews is, "what is difference between Vector and ArrayList?" You might have seen this in on your own interviews as well. Though both are implementation of List interface from JDK 1.4 onward, the key difference between them is Vector is not a thread-safe and synchronized Collection but ArrayList is not synchronized. Which means if multiple threads tries to add and remove elements from Vector, it will still be Ok, but ArrayList's structure may be destroyed. If you remember this key factor then you can automatically derive several other differences between Vector and ArrayList las shown below.
Sunday, 23 August 2015
Difference between Comparator and Comparable in Java
Here are main difference between Comparator and Comparable in Java :
1) Comparable is used to define natural order of object in Java e.g. numeric order for numbers, lexicographic order for String etc, and Comparator is used to define custom order which in addition of default ordering provided by Comparable e.g. default ordering for an Employee class could be by name or id, but you can provide additional Comparator to compare them by salary, age and branch.
1) Comparable is used to define natural order of object in Java e.g. numeric order for numbers, lexicographic order for String etc, and Comparator is used to define custom order which in addition of default ordering provided by Comparable e.g. default ordering for an Employee class could be by name or id, but you can provide additional Comparator to compare them by salary, age and branch.
Monday, 3 August 2015
What are the difference between Having and Where Clause?
Here are some useful difference between WHERE and HAVING clause in SQL :
1) WHERE clause can be used with SELECT, UPDATE and DELETE statements and clauses but HAVING clause can only be used with SELECT statements.
e.g.
SELECT * FROM Employee WHERE EmployeeId=3
will print details of employee with id = 3.
1) WHERE clause can be used with SELECT, UPDATE and DELETE statements and clauses but HAVING clause can only be used with SELECT statements.
e.g.
SELECT * FROM Employee WHERE EmployeeId=3
will print details of employee with id = 3.
Subscribe to:
Posts (Atom)