Set Interface and Map Interface in java.util Package

Introduction

The Set interface extends the Collection interface. The set does not allow duplicates in the collection. In Set implementations null is a valid entry, but allowed only once. The map is a special kind of set with no duplicates. In Collections API, java.util.Map defines this interface. It Maps the key values against the records stored in the database.

Two classes that implement the Set Interface

There are basically two classes that implement the Set interface. They are,

  • HashSet
  • TreeSet

The HashSet Class

This implements java.util.Set interface to eliminate the duplicate entries and use hashing for storage. Hashing is nothing but mapping between a key value and a data item. This provides efficient searching.

Source Code

import java.util.*;
public class TestHashSet {
public static void main(String args[])
{
Set set=new HashSet();
set.add("Ashish");
set.add("Sonali");
set.add("Bhatnagar");
set.add("C# Corner");
set.add("Java");
set.add("Ashish");
System.out.println(set);
}
}

The output of the program is given below. Note that the name “Ashish” that appears twice in the code has not been added to the HashSet.

HashSet

The TreeSet Class

This implements java.util.Set interface provides an ordered set, eliminates duplicate entries, and uses a tree for storage. A tree is nothing but a linked list that allows elements to be added, and removed at any location in the container by insisting means of ordering.

Source Code

import java.util.*;
public class TestTreeSet {
public static void main(String args[])
{
Set set=new TreeSet();
set.add("Ashish");
set.add("Sonali");
set.add("Bhatnagar");
set.add("C# Corner");
set.add("Java");
set.add("Ashish");
System.out.println(set);
}
}

The output of the program displays a list of sorted names as shown as follows.

Program displays

Map Interface

A map is a special kind of set with no duplicates. In the Collections API, java.util.Map defines this interface. It maps the key values against the records stored in the database. The key values are used to look, at or index the stored data. The Map interface is not an extension Collection interface, it has its own hierarchy. The map does not allow duplicates in the collection. In Map implementations null is a valid entry, but allowed only once.
There are two Map implementations available in the Collections Framework:

  • HarshMap
  • TreeMap

The HashMap Class

This implements java.util.Map interface and uses hashing for storage. Indirectly Map uses Set functionality. Hence, it does not permit duplicates.

The TreeMap Class

This implements java.util.Map the interface and use the tree for storage. It provides the ordered map. The following program uses both the HashMap and TreeMap classes to display a list of names associated with keys. The Map Interface contains methods to add, remove, find, and get objects using various methods. The program given here uses the following methods.

  • void put(Object key, Object Value): This method associates the specified values with the specified key in this map.
  • void putAll(Map t): Copies all the mappings from the specified map to this map.
  • boolean contains value (Object Value): Returns true if this maps one or more keys to the specified value.
  • Object get(Object key): Returns the value to which this map maps the specified key.

There are other methods in the Map interface like containsKey(), clear(), size(), remove(), etc. that perform various functions.

Source Code

import java.util.*;
public class MapTest {
    public static void test(Map map) {
        map.put("a1", "Ashish");
        map.put("a2", "Sonali");
        map.put("a3", "Bhatnagar");
        map.put("a4", "C# Corner");
        map.put("a5", "Java");
        map.put("a6", "Ashish");
        System.out.println(map); // No duplicates
        // put another map to this one
        map.putAll(map);
        map.put("a7", "Heroz");
        map.put("a7", "Singing");
        map.put("a7", "Programming");
        System.out.println(map);
        System.out.println("map.contains(\"Singing\"): " + map.containsValue("Singing"));
        System.out.println("Get the first element: " + map.get("a1"));
    }
    public static void main(String[] args) {
        System.out.println("HashMap");
        test(new HashMap());
        System.out.println("TreeMap");
        test(new TreeMap());
    }
}

The output of the above program will be as,

Output

Summary

The Set interface, an extension of the Collection interface, ensures that all elements in a collection are unique, prohibiting duplicates. Within Set implementations, null values are permissible but only as a single entry. The Map interface is a specialized form of Set that also disallows duplicates. In the Java Collections API, the java.util.Map interface manages the mapping of key values to corresponding records in a database, providing an organized way to retrieve and store data efficiently.


Similar Articles