Collections in Java

The Collections framework of  Java contains data structures like set,list,map etc.Collections in Java  enables us to manipulate group of objects .So a   developer can use any of the structure while writing the application code.There are lot of classes and interfaces in Collections framework.The important interfaces and classes are shown below as a hierarchy layout.

Collections Framework in Java – Block Diagram

Important classes & Interfaces in Java

Important classes & Interfaces in Collections

There is Collection interface.List and Set interfaces extending from Collection interface.

1.List interface

All List implementations are ordered by index position.The key methods in List interface are related to index.The methods include get(int index) , add(int index, Object arg)..etc.

a)ArrayList :ArrayList implements  List interface. It implements the marker interface randomAccess too.ArrayList gives faster iteration.Since it is implementing RandomAccess it provides faster random access too.

b)LinkedList :In LinkedList  the elements are doubly  linked to each other. LinkedList is slower in iteration when compared with ArrayList . But insertion and deletion are faster in LinkedList. In latest versions of Java , LinkedList implements Queue interface too.So LinkedList is having properties of Queue also.

c)Vector: It is one of the legacy class with Java. Vector is almost similar to ArrayList.The difference is vector methods are thread safe. This causes a performance downfall for Vector when compared with ArrayList.

2)Set interface

Set never allows duplication.Set uses equals() method to check whether two objects are equal or not.  The classes which implements Set are listed below.

a)HashSet : In a HashSet items are arranged in a random order. The efficiency of this structure depends on the hashCode() implementation of objects inserted.

b)LinkedHashSet:It is the LinkedList version of  HashSet. In LinkedHashSet the order of insertion is preserving.The efficiency of this structure also depends on the hashCode() implementation of objects being inserted.

c)TreeSet:It is a sorted collection.Default sorting order is ascending order.Using Comparable or Comparator instances , it is possible to change the order.

3)Map Interface

In Map data is storing as  unique keys vs data .Remember, Map is not extending from Collection interface. Like Set ,Map also relying on equals() method to check whether two keys are equal or not.The classes which implements Map are listed below.

a)HashMap : It is an unsorted and unordered map implementation. In Hashset data is storing in a random order of keys.The efficiency of HashMap depends on the  hashCode() implementation of objects using as keys.HashMap allowas one null key and many null values.

b)LinkedHashMap : It is the LinkedList version of  HashMap.The order of insertion is preserving in LinkedHashMap. This the difference between HashMap and LinkedHashMap.

c)Hashtable : It is a legacy class in Java.Hashtable is the thread safe version of HashMap.We know HashMap allows one null key and many null values. But hashtable never allows both.That is the difference between HashMap and Hashtable.

d)TreeMap : It is a sorted map. The default sorting order is ascending order. The order depends on the Comparable or Comparator implementations.

See Related Discussions:

ArrayList

Linked List

Hashtable

Vector

Concurrent Collections

TreeSet in Java

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">