Difference between ArrayList and LinkedList in Java

ArrayList and LinkedList both implements List interface and their methods and results are almost identical. However there are few differences between them which make one better over another depending on the requirement.

ArrayList Vs LinkedList

1) Search: ArrayList search operation is pretty fast compared to the LinkedList search operation. get(int index) in ArrayList gives the performance of O(1)while LinkedList performance is O(n).

Reason: ArrayList maintains index based system for its elements as it uses array data structure implicitly which makes it faster for searching an element in the list. On the other side LinkedList implements doubly linked list which requires the traversal through all the elements for searching an element.

2) Deletion: LinkedList remove operation gives O(1) performance while ArrayList gives variable performance: O(n) in worst case (while removing first element) and O(1) in best case (While removing last element).

Conclusion: LinkedList element deletion is faster compared to ArrayList.

Reason: LinkedList’s each element maintains two pointers (addresses) which points to the both neighbor elements in the list. Hence removal only requires change in the pointer location in the two neighbor nodes (elements) of the node which is going to be removed. While In ArrayList all the elements need to be shifted to fill out the space created by removed element.

3) Inserts Performance: LinkedList add method gives O(1) performance while ArrayList gives O(n) in worst case. Reason is same as explained for remove.

4) Memory Overhead: ArrayList maintains indexes and element data while LinkedList maintains element data and two pointers for neighbor nodes hence the memory consumption is high in LinkedList comparatively.

There are few similarities between these classes which are as follows:

  1. Both ArrayList and LinkedList are implementation of List interface.
  2. They both maintain the elements insertion order which means while displaying ArrayList and LinkedList elements the result set would be having the same order in which the elements got inserted into the List.
  3. Both these classes are non-synchronized and can be made synchronized explicitly by using Collections.synchronizedList method.
  4. The iterator and listIterator returned by these classes are fail-fast (if list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException).

When to use LinkedList and when to use ArrayList?

1) As explained above the insert and remove operations give good performance (O(1)) in LinkedList compared to ArrayList(O(n)). Hence if there is a requirement of frequent addition and deletion in application then LinkedList is a best choice.

2) Search (get method) operations are fast in Arraylist (O(1)) but not in LinkedList (O(n)) so If there are less add and remove operations and more search operations requirement, ArrayList would be your best bet.

Courtesy: http://beginnersbook.com/2013/12/difference-between-arraylist-and-linkedlist-in-java/

How to format Date in Java – SimpleDateFormat Example

SimpleDateFormat in Java is used to format Date in Java. You can format date on any String format based upon various attribute available in SimpleDateFormat class e.g. mm, dd, YYetc. You can also put timezone information in formatted Date using Z attribute of DateFormat class. SimpleDateFormat is sub class of DateFormat and provide format() and parse()method to convert Date to and from String in Java. Worth noting is that SimpleDateFormat is not thread-safe and should not be shared with others. Avoid using static SimpleDateFormat inJava classes. If you want to share SimpleDateFormat or want to make it thread-safe, you can use ThreadLocal variable in Java to avoid sharing SimpleDateFormat among multiple threads.parse() method of SimpleDateFormat throws ParseException if String input is not a valid date or can not be parsed into mentioned format.

How to format Date in Java

In order to format dates using SimpleDateFormat, we first needs to define a String date format e.g. “dd-MM-yyyy”will print dates in that format e.g. 01-11-2012. You can defined format based upon identifiers supported bySimpleDateFormat class. e.g. d means day of month, y means year and M means Month of year. Javadoc ofSimpleDateFormat has complete list of supported Date and Time patterns . Once you create a DateFormat, you can just call format() method which accept java.util.Date and returns String, which is formatted Date. In this way you can alsoconvert Date to String in Java. SimpleDateFormat formats date in same pattern which is provided to it while creating instance ofSimpleDateFormat. You can also include time information e.g. hour, minutes and seconds while formatting dates by using HH, mm and SS time pattern. DateFormat class also supports inclusion of timezone in formatted date String by z and Z. If you use z you can show timezone information as abbreviation e.g. PST, IST etc. If you use Z you provide timezone, relative to GMT e.g.+0530. In next section we will see couple of SimpleDateFormat example in Java to get hands on on date formatting.

SimpleDateFormat Example in Java

Here is complete code example of How to format Date in Java using SimpleDateFormat. In this example we will see simple date formatting without time information e.g. dd-MM-yyyy, Date formatting with time information with patterns like dd-MM-yyyy:HH:mm:SS and Dates with timezone information in it e.g. dd-MM-yyyy:HH:mm:SS z or dd-MM-yyyy:HH:mm:SS Z.

import java.text.SimpleDateFormat;
import java.util.Date;

* Java program to show how to format date in Java using SimpleDateFormat
* Examples. Java allows to include date, time and timezone information
* while formatting dates in Java.
* @author http://java67.blogspot.com
public class DateFormatExample {

public static void main(String args[]) {

// This is how to get today’s date in Java
Date today = new Date();

//If you print Date, you will get un formatted output
System.out.println(“Today is : ” + today);

//formatting date in Java using SimpleDateFormat
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(“dd-MM-yyyy”);
String date = DATE_FORMAT.format(today);
System.out.println(“Today in dd-MM-yyyy format : ” + date);

//Another Example of formatting Date in Java using SimpleDateFormat
DATE_FORMAT = new SimpleDateFormat(“dd/MM/yy”);
date = DATE_FORMAT.format(today);
System.out.println(“Today in dd/MM/yy pattern : ” + date);

//formatting Date with time information
DATE_FORMAT = new SimpleDateFormat(“dd-MM-yy:HH:mm:SS”);
date = DATE_FORMAT.format(today);
System.out.println(“Today in dd-MM-yy:HH:mm:SS : ” + date);

//SimpleDateFormat example – Date with timezone information
DATE_FORMAT = new SimpleDateFormat(“dd-MM-yy:HH:mm:SS Z”);
date = DATE_FORMAT.format(today);
System.out.println(“Today in dd-MM-yy:HH:mm:SSZ : ” + date);



Today is : Fri Nov 02 16:11:27 IST 2012
Today in dd-MM-yyyy format : 02-11-2012
Today in dd/MM/yy pattern : 02/11/12
Today in dd-MM-yy:HH:mm:SS : 02-11-12:16:11:316
Today in dd-MM-yy:HH:mm:SSZ : 02-11-12:16:11:316 +0530

That’s all on these SimpleDateFormat Example in Java. We have seen How to format date with time and timezone information in Java. Though using SimpleDateFormat is most easy way to format Date in Java but it also has its own set of problems. It’s notthread-safe and should be used carefully. Avoid using DateFormat as static variable, don’t share SimpleDateFormat betweenmultiple threads, result is unpredictable if it’s shared among multiple threads.

Courtesy : http://java67.blogspot.in/2013/01/how-to-format-date-in-java-simpledateformat-example.html