Wednesday, May 2, 2007

Explain List,Set,Map?

The three most significant types of Collections framework are:

First,look into the hierarchy of collection interfaces:

java.util.Collection <---------java.util.List
java.util.Collection <--------- java.util.Set
java.util.Collection <--------- java.util.SortedSet
Collection interface is the root interface of collection hierarchy.List interface is most commonly used collection type.LinkedList and ArrayList are its well known implementing classes.Lists can store objects only and not primitive types like int but one can create Integer objects.All objects in List are indexed from 0 to (size of list-1)

Set is very much like list but with added constraint of not storing duplicate values.Sets do not impose a 0..size-1 indexing of the elements (that's what Lists do), so List methods like get(int index) are not available for sets.

HashSet is mostly used set which only works with elements, like String and Integer, which have a hashCode() defined. The TreeSet is an alternative which has performance issues, but keeps the set in sorted order, so iteration will yield the values in sorted order.

java.util.Collection <-----------java.util.Map

A Map is altogether different from List and Set.It stores key-value pairs and any value can quickly be searched on the basis of a key.A map cannot contain duplicate keys; each key can map to at most one value.'Map' is a basic interface being implemented by classes HashMap and TreeMap.HashMap is most commonly used which can store objects in unordered fashion while TreeMap can store in ordered fashion.Some implementations of Map prohibit null keys and values and some have restrictions on type of their keys.You may get NullPointerException or ClassCastException when trying to insert or retrieve invalid keys or values.
Read More on Collection Framework...


Sasi Kumar said...

Explanation given was easy to understand.Best answer.

Anonymous said...

Straight to the point! Thanks.

Anonymous said...

Good explaination !! thanks

Mohan kumar on April 6, 2010 at 11:59 PM said...

its very straight ...kind of simplified datastructure explanations

Anonymous said...

good one..short n sweet

Anonymous said...

Nice n easy!! good!!!

Anonymous said...

MAP is NOT a part of the Collection Faramework

Dharmendra P on January 19, 2013 at 10:48 AM said...

Map is not extending Collection interface. It is altogether a separate interface. For more details, please refer:


Interview Questions On Java,Java EE Copyright © 2017. Reads: best tracker