Home » Java programming language

Differences between Set and Map interface in Java

Set vs Map interface in Java: Here, we are going to learn what are the differences between Set and Map interface in Java programming language?
Submitted by Preeti Jain, on August 07, 2019

Set vs Map interface

Here, we will see how Map differs from the Set interface in Java and we will see the points given below,

Map interface

  • The Map is an interface that is defined in java.util package.
  • The Map is the data structure in Java.
  • The Map is based on Hashing and The Map object is represented in the form of key-value pairs and the key-value pairs are called entry.
  • The performance of Map interface is high as compare to Set interface.
  • In the case of Map interface, there is no collision concept if we know keys.
  • The implementation class of Map interface is HashMap, LinkedHashMap, and ConcurrentHashMap, etc.
  • The Map is different from Collection or in other words, there is no relation between Map and Collection (i.e. It is not a child interface of Collection interface because Map does not implement Collection interface).
  • The Map does not provide uniqueness fully (i.e. Duplicates are not allowed for Keys and Duplicates are allowed for values).
  • We should go for Map if we want to represent a group of the object as key-value pairs.
  • The Map is meant for a group of key-value pairs.

Example:

Let suppose we have a Map with few elements. Here we are adding the elements in the order is {Java=1000, C=2000, C++=3000, Ruby=4000, Python=1000,null=null, Django=null, null=7000} and if we are retrieving the elements so the order of retrieving elements can be different (i.e. insertion order is not preserved and it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like {Ruby=4000, C=2000, Django=null, Python=1000, C++=3000, null=7000, Java=1000}

// Java program to demonstrate the behavior of Map

import java.util.Collection;
import java.util.Map;
import java.util.HashMap;

class MapClass {
    public static void main(String[] args) {
        // Creating an instance of HashMap
        Map map = new HashMap();

        //By using put() method to add some values in Map
        map.put("Java", 1000);
        map.put("C", 2000);
        map.put("C++", 3000);
        map.put("Ruby", 4000);
        map.put("Python", 1000);
        map.put("null", null);
        map.put("Django", null);

        /* Here we will not get any error but one null is accepted for keys*/
        map.put("null", 7000);

        // Display retrieval order of Map
        System.out.println("Current Map list is :" + map);

        // by using values() to find values of Map
        Collection values = map.values();

        // Display Values of Map
        System.out.println("Current Map Key values is :" + values);
    }
}

Output

E:\Programs>javac MapClass.java

E:\Programs>java MapClass
Current Map list is :{Ruby=4000, C=2000, Django=null, 
Python=1000, C++=3000, null=7000, Java=1000}
Current Map Key values is :[4000, 2000, null, 1000, 3000, 7000, 1000]

Now, we will see how set differs from Map interface in Java and we will see the points given below,

Set interface

  • Set is an interface that is defined in java.util package. 
  • Set is the data structure in Java.
  • The Set object is represented in the form of values. 
  • The performance of Set interface is low as compare to Map interface. 
  • In the case of the Set interface, there may be collision concepts. 
  • The implementation class of Set interface is HashSet, LinkedHashSet, etc.
  • Set is not different from Collection or in other words there is a relation between Set and Collection (i.e. It is a child interface of Collection interface because Set implements Collection interface).
  • Set provides uniqueness (i.e. Duplicates are not allowed or we cannot insert one object multiple times). 
  • We should go for Set if we want to represent a group of the object as a single entity. 
  • Set is meant for a group of individual objects.

Example:

Let suppose we have a Set with few elements. Here we are adding the elements in the order is [10,20,30,50, null] and if we are retrieving the elements so the order of retrieving elements can be different (i.e. it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like [null.50,20,10,30].

// Java program to demonstrate the behavior of Set interface 

import java.util.*;

class SetInterface {
    public static void main(String[] args) {
        // Creating an instance
        Set set = new HashSet();

        // By using add() method to add an elements
        set.add(10);
        set.add(20);
        set.add(30);
        set.add(50);
        set.add(null);

        // set.add(20); 
        // if we add again 20 then we will not get any error but 
        // duplicate element will be ignored

        // Display Set elements
        System.out.println("Retrieval order of the elements in Set is :" + set);
    }
}

Output

E:\Programs>javac SetInterface.java

E:\Programs>java SetInterface
Retrieval order of the elements in Set is :[null, 50, 20, 10, 30]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.