Difference between Set, List and Map Collections in java?

 

Set (Interface)
A collection that contains no duplicate elements.
We can iterate the values by calling iterator() method. Since java 1.2 version.

Set s1 = new HashSet();
 Iterator itr = s.iterator();

List (Interface)
An ordered collection (also known as a sequence). Allows duplicate elements.
We can iterate the values by calling iterator() method. Since java 1.2 version.

List l1 = new ArrayList();
 Iterator itr = li.iterator();

Map(Interface)
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface.
In Map we don’t have iterator() method, but we can get the keys by calling the method keySet().

 
 Map m1; // insert values
 Set s1 = m1.keySet();
 // Get Map keys into the Set and then iterate this Set object normally
 // m1.keySet() returns Set object with Map keys
 Iterator itr = s1.iterator();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s