Explain List,Set,Map?
The three most significant types of Collections framework are:
-List
-Set
-Map
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...
-List
-Set
-Map
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...