Friday 28 January 2011

Java: Collections basics


In this article i'll describe basics of java collections


Interface List - an ordered collection. The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index.
ArrayList 
An ArrayList is better than Array to use when you have no knowledge in advance about elements number. ArrayList are slower than Arrays. So, if you need efficiency try to use Arrays if possible.Also it is not synchronized. The add operation runs O(n) time.Permits null elements
Vector
Very similar to the ArrayList class. Slover than  ArrayList .Also it is synchronized.
LinkedList
LinkedList is much more flexible and lets you insert, add and remove elements from both sides of your collection - it can be used as queue and even double-ended queue.Manipulation with data is fast but showing or getting some element only after enumeration all.


Interface Set - A collection that contains no duplicate elements. This interface models the mathematical set abstraction.
HashSet
Set is a collection of distinct objects.It stores its elements in a hash table.Order -undefined.Performance - better than LinkedHashSet
TreeSet 
 It stores its elements in a red-black tree.Order - ascending.Performance - Slow


Interface Map - A collection that contains objects with keys to values mapping.
HashMap 
A hash table or hash map is a data structure that uses a hash function to map identifying values, known as keys(Key -- Value) HashMap is not synchronized.HashMap allows one null key and any number of null values.
TreeMap
is the sorted version of the HashMap, offers the ability to traverse the contents of the Map in a determined order.
HashTable
Is very similar to the HashMap. It's synchronized.Hashtable does not allow null keys or values.
LinkedHashSet 
 is implemented as a hash table with a linked list running through it.Order - insertion.Performance - has fast adding to the start of the list, and fast deletion from the interior via iteration

References:
TreeSet vs HashSet vs LinkedHashSet 
Array vs ArrayList vs LinkedList vs Vector 
Java Collections

No comments:

Post a Comment