Friday, July 1, 2011

Collection in Java

Collection is a framework for keeping a group of objects. It is not synchronized. They are basically of two types: list and set. Some of them are ordered while some allow duplicates. List can contain duplicate elements while set cannot

Features of List
  1. Ordered
  2. Eg: Arraylist, linkedlist

ArrayList
  1. positional access : Constant time
  2. Addition and deletion: Linear time
LinkedList
  1. positional access : Linear time
  2. Addition and deletion: Constant time

Features of Set
  1. Eg. HashSet, TreeSet

HashSet
  1. Faster but offers no ordering guarantee
  2. Since the iteration is linear in sum of number of entries and capacity, choosing the size of hashset makes the code faster

If we need to use the operations in SortedSet or if in-order iteration is important to you, use TreeSet.

Since their is no synchronization in Collection, it is generally faster than Hashtable and Vector.

More information is available at following link

No comments:

Post a Comment