Thursday, 22 December 2011

Java Collections



Difference between ArrayList and LinkedList ?
ArrayList
LinkedList
Random Access [Reading]
Sequential Access [Reading]
Add/Remove elements only at the end [Writing]
Add/Remove elements frequently from the middle of the list [Writing]
Insert/Deletion of the elements is slow.
Insert/Deletion is fast


Difference between ArrayList and Vector ?
ArrayList
Vector
ArrayList is NOT synchronized by default.
Vector List is synchronized by default.
ArrayList can use only Iterator to access the elements.
Vector list can use Iterator and Enumeration Interface to access the elements.
The ArrayList increases its array size by 50 percent if it runs out of room.
A Vector defaults to doubling the size of its array if it runs out of room
ArrayList has no default size.
While vector has a default size of 10.

Difference between List and Set?
List
Set
stores elements in an ordered way
stores elements in an unordered way
may contain duplicate elements
does not contain duplicate elements
allows multiple null element.
allows only one null element



Difference between HashSet and TreeSet ?
HashSet
TreeSet
HashSet does not guarantee for either sorted order or sequence order.
TreeSet provides elements in a sorted  order (ascending order).
We can add any type of elements to hash set.
We can add only similar types of elements to tree set.
hashSet organizes data in a hash table (using a hash function).
A treeSet organizes data in a tree through use of Comparator (natural ordering)





TreeSet
HashSet
LinkedHashSet
public class TreeSet
extends AbstractSet
public class HashSet
extends AbstractSet
public classLinkedHashSet
extends HashSet
unique values
unique values
Unique values
It stores its elements in a red-black tree through use of Comparator (natural ordering)
It stores its elements in a hash table(using a hash function).
is implemented as a hash table with a linked list running through it
Order : ascending order
undefined
insertion order
Performance : Slow
better than LinkedHashSet
has fast adding to the start of the list, and fast deletion from the interior via iteration
operations (addremove and contains)
operations (addremove,contains and size)
operations (add,contains and remove)

HashMap
HashTable
TreeMap
LinkedHashMap
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable,

public class Hashtable<K,V>
extends Dictionary<K,V>
implements Map<K,V>, 

public class
TreeMap<K,V>
extends
implements
 NavigableMap<K,V>,

public class
LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>

Unidentified Order
Descending Order
Ascending Order
Insertion Order
UnSynchronized
Synchronized
UnSynchronized
UnSynchronized
HashMap allows multiple null values and one null as a key as the key should be unique
Hasttable does not allow any null value as either key or value.
The values can be null. But the key should be unique
The values can be null. But the key should be unique

What are the differences between the Comparable and Comparator interfaces ?
Comparable
Comparator
It uses the compareTo() method.
int objectOne.compareTo(objectTwo).
t uses the compare() method.

int compare(ObjOne, ObjTwo)
It is necessary to modify the class whose instance is going to be sorted.
A separate class can be created in order to sort the instances.
Only one sort sequence can be created.
Many sort sequences can be created.
It is frequently used by the API classes.
It used by third-party classes to sort instances.



The symbols shown in the figure above define interfaces.

Corresponding Implementations

Implementations
Hash Table
Resizable Array
Balanced Tree
Linked List
Interfaces
Collection
HashSet
ArrayList
TreeSet
LinkedList
Set
HashSet

TreeSet
LinkedSet 
SortedSet


TreeSet

List

ArrayList

LinkedList
Map
HashMap

TreeMap

SortedMap


TreeMap









Java Collection

No comments:

Post a Comment