Role Of Map Interface And Iterator Interface In Java

Introduction

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 generally used to lookup, or index the stored data. The Map interface is not an extension Collection interface, It has its own hierarchy. Map does not allow duplicates in the collection. In Map implementation null is a valid entry, but allowed only once.

Types of Map Interface

Basically two types of Map Implementations are available in Collections Framework:

  • HashMap
  • TreeMap

The HashMap class

The HashMap class implements java.util.Map interface & uses hashing for storage. Indirectly Map uses Set functionality. Hence it does not permit duplicates.

The TreeMap class

The TreeMap class implements java.util.Map interface & uses 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 value with the specified key in this map.

void putAll(Map t)

Copies all the mappings from the specified map to this map.

Boolean containsValue(Object Value)

It returns true, if this map maps one or more keys to the specified value.

Object get(Object key)

It also 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", "Ashu");
        map.put("a4", "Sonu");
        map.put("a5", "Ashu");
        System.out.println(map); //No duplicates
        //put another map to this one
        map.putAll(map);
        map.put("a6", "Robin");
        map.put("a7", "Sonia");
        map.put("a8", "Aman");
        System.out.println(map);
        System.out.println("map.contains(\"Sonali\");" + map.containsValue("Sonali"));
        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());
    }
}

Output

The Iterator Interface

An Iterator is similar to the Enumeration interface. With this interface methods, you can traverse a collection from start to end & safely remove elements from underlying Collection. The iterator() method is basically used in query operations.

Basic Methods

  • iterator.remove();
  • iterator.hasNext();
  • iterator.next();

Let us write a program that uses iterator. The benefit of using iterators in your program is that you do not need to worry about the number of elements in the container. This is taken care of by hasNext() and next() methods.

Source Code

import java.util.*;
class Student {
    private int StudentNumber;
    Student(int i) {
        StudentNumber = i;
    }
    void print() {
        System.out.println("Student #" + StudentNumber);
    }
}
public class IteratorTest {
    public static void main(String args[]) {
        ArrayList st = new ArrayList();
        for (int i = 0; i < 7; i++) st.add(new Student(i));
        st.add(new Student(7));
        Iterator e = st.iterator();
        while (e.hasNext()) {
            ((Student) e.next()).print();
        }
    }
}

Output

Summary

The Map interface is not an extension Collection interface, It has its own hierarchy. The Map interface contains methods to add, remove, find and get objects using various methods. An Iterator is similar to the Enumeration interface.