Newsletter |
Difference between Java Set, List and Map Collections?
Core Java » on Oct 21, 2012 { 14 Comments } By Sivateja
Let us see the main differences between Set,List,Map java collections..
Set (Interface)
- Set is an un-ordered collection which doesn’t allows duplicate (no-duplicate) elements
- We can iterate the values by calling iterator() method
Set s = new HashSet();
Iterator iter = s.iterator();
List (Interface)
- List is an ordered collection which allows duplicate elements
- We can iterate the values by calling iterator() method
List li = new ArrayList();
Iterator iter = li.iterator();
Map (Interface)
- In Map we used to store the data in key and value pairs, we may have duplicate values but no duplicate keys
- In Map we don’t have iterator() method, but we can get the keys by calling the method keySet()
Map m; // insert values Set s = m.keySet(); // Get Map keys into the Set and then iterate this Set object normally // m.keySet() returns Set object with Map keys Iterator iter = s.iterator();
You Might Also Like
::. About the Author .:: | ||
Comments
14 Responses to “Difference between Java Set, List and Map Collections?”
Hi sir, could u please explain about jUnit.
Please make a small correction while explaining about List you misspelled ‘Set’ instead List.
– Modified
Please tell the answers in real time scenarios.
sir pls explain more details in collection with example
Hi Sir!, wait,notify,notifyall methods are related to Thread class then why they are found in Object class,Please explain…..I am waiting …
wait,notify,notifyall methods are called on object(user defined like studentObject).so theses methods are defined in Object class
Sir,
please explain about arraylist and hashcode & map
Nice content for collection concept.
Hi ,
Could you please explain about JUnit with example ?
Thanks In Advance,
Prasad
sir pls explain more details about collection with example
IF HashSet internally implements HashMap, then what is the purpose of HashSet as a separate Class when ultimately every task is done by HashMap itself? Can anyone please sort this confusion for me????
Sir, Please explain Comparator and Comparable Interfaces.
Arrayist:
1. It preserves the insertion order.
2. retrieving elements is very vast.
3. It is internal index based mechanism(It internally follows the arrays)
4. insertion,deletion of elements in the ArrayList is slow. because of it follows the index base. if you remove one element in the list. it will change the index.
Array is of fixed size and is a collection of similar data type. Collection has a dynamic size and support heterogeneous datatypes. Thanks for sharing this difference