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 .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

14 Responses to “Difference between Java Set, List and Map Collections?”
  1. hussain says:

    Hi sir, could u please explain about jUnit.

  2. kiran says:

    Please make a small correction while explaining about List you misspelled ‘Set’ instead List.

    – Modified

  3. praveen says:

    Please tell the answers in real time scenarios.

  4. vinayak says:

    sir pls explain more details in collection with example

  5. Ajit says:

    Hi Sir!, wait,notify,notifyall methods are related to Thread class then why they are found in Object class,Please explain…..I am waiting …

    • Ganesh says:

      wait,notify,notifyall methods are called on object(user defined like studentObject).so theses methods are defined in Object class

  6. suresh says:

    Sir,
    please explain about arraylist and hashcode & map

  7. Selva says:

    Nice content for collection concept.

  8. prasad says:

    Hi ,

    Could you please explain about JUnit with example ?

    Thanks In Advance,
    Prasad

  9. bhavana says:

    sir pls explain more details about collection with example

  10. Shubham Singh Parmar says:

    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????

  11. Manish says:

    Sir, Please explain Comparator and Comparable Interfaces.

  12. Ganesh says:

    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.

  13. priya says:

    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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.