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.
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
Very similar to the ArrayList class. Slover than  ArrayList .Also it is synchronized.
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.
Set is a collection of distinct objects.It stores its elements in a hash table.Order -undefined.Performance - better than LinkedHashSet
 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.
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.
is the sorted version of the HashMap, offers the ability to traverse the contents of the Map in a determined order.
Is very similar to the HashMap. It's synchronized.Hashtable does not allow null keys or values.
 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

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

No comments:

Post a Comment