1:59 pm - Thursday March 21, 2019

Collections in java

Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

What is collections?

Collections are an interface in the java.util package, and it is used to define a collection of objects. There are many different classes which implement the collections interface. Each one has its unique features, some add elements in a sorted manner, others in a binary fashion. But there are two basic structures, a map and a list and most others extended these interfaces.

 Hierarchy Structure of Collection: 

A collection has three categories. 





» List is used to store collection of objects.

» List allows duplicates.

» Set also used to store collection of objects.

» Set does not allow duplicates.

» Map is used to store key-value pairs.


ArrayList : 

» ArrayList class is sub class of list which is implementing marker interfaces called random access clonable and serializable.

» ArrayList is used to store group of objects similar or dissimilar objects.

» ArrayList allows duplicates.

» Objects in the array list will be store internally using index notation.

» ArrayList is not synchronized by default because it is a collection class.


Vector :

» Vector is a sub class of list whose functionality is very similar to array list.

» Sun has reengineered vector has a sub class of list to make use of functionality of list and collection interfaces.

» The main difference between vector and array list is “vector is synchronized by default where as array list is not synchronized by default”.

Difference between ArrayList and Vector List :


Vector List

1.ArrayList is not synchronized by default. 1.Vector List is synchronized by default.
2.ArrayList can use only Iterator to access
the elements.
2.Vector list can use Iterator and Enumeration
Interface to access the elements.
3.ArrayList is implementing Random Access
marker interface because of this we can pick
the elements
3.Vector list is implements random access
marker interface. Because of this we can
pickup the elements randomly from vector
4.JVM using Index notation to store the array
list internally.
4.JVM will use Index notation to store the
vector list internally.


Set :

» Set is a collection of interface which does not allow duplicates.

» This interface has three sub classes.


1.Hash Set

2.Tree Set

3.Linked Hash Set

Difference between Hash set and Tree Set :



1.HashSet is under set interface i.e. it
does not guarantee for either sorted
order or sequence order.
1.TreeSet is under set i.e. it
provides elements in a sorted
order (acceding order).
2.We can add any type of elements
to hash set.
2.We can add only similar types
of elements to tree set.



Map Interface :

» Map interface is used to store key-value pairs.

» Map is a part of collection framework but it not under collection interface.


Sub-class of Map :

1.Hash Map





HashMap :

» HashMap is an unordered set.

» HashMap does not allow duplicate keys and is allow duplicate values.

» HashMap will allow null keys and null values.


Hash Table :

» Hashtable is a legacy class which was recognized as a subclass of map interface.

» Hashtable does not allow null values.


Difference between HashMap and HashTable:



1.HashMap is not synchronized by default. 1.Hashtable is synchronized by default.
2.HashMap allows null keys. 2.Hashtable does not allow null keys.
3.HashMap allows null values 3.Hashtable does not allow null values.




Similarities of Hash map and Hash table :

» Both HashMap and Hashtable does not allows duplicate keys but both allow duplicate values.

» Both HashMap and Hashtable are unordered maps.


TreeMap :

» TreeMap is a sorted map i.e. which gives the entries in sorted order using keys.

» Null keys are not allowed.

» Duplicate keys are not allowed.

» Null values are allowed.

» Duplicate values are allowed.

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply