Java Tutorial

Map

Two dimensional framework or maps:

Two dimensional framework organize the data in the form of (key,value) pair. The value of key is an object and they must be unique. The value of value is also an object which may or may not be unique. Two dimensional framework contains collection of interfaces and collection of classes which are also known as map interfaces and map classes.

Map interfaces:

In maps we have three essential interfaces; they are java.util.Map, java.util.Map.Entry and java.util.SortedMap

java.util.Map:

java.util.Map extends Collection. An object of Map allows to organize the data in the form of (key, value) pair. Here key and value must be objects. An object of Map allows displaying the data in that order in which order we have added the data.

Methods:

public booleanput (Objectkobj,Objectvobj): This method is used for adding the data in the form of (key, value). This method returns false when we are trying to add duplicate key and values. This method returns true as long as we enter unique key objects.

public boolean putAll (Map): This method is used for adding one Map object at the end of another Map object.

public Set entrySet (): This method is used for obtaining the data of the map in the form of Set object.

public Object get (Object vobj): This method is used for obtaining value of value by passing value of key object.

public void remove (Object kobj): This method is used for removing the entire map entry by passing the value of key object.

two dimensional frame work

Note:The following diagram gives an idea about how to organize the data in the form of (key,value) and how to retrieve the data from Map object.

java.util.Map.Entry:

Here Map is an interface and Entry is the class in Map interface. java.util.Map.Entry is used for retrieving the data separately in the form of key object and value object from the Map object.

Methods:

public Object getKey (); 71 
public Object getValue (); 72

Method 1 and 2 is used for obtaining key object and value object separately.

java.util.SortedSet:

SortedMap extends Map. An object of SortedMap displays the data by default in sorted order.

Methods:

public Object first (); 
public Object last ();
public SortedSet headMap (Object kobj); xi <= kobj
public SortedSet tailMap (Object kobj); xi > kobj
public SortedSet subMap (Object kobj, Object vobj); kobj1 <= xi < kobj2

Map classes:

Map classes contains all the definitions for the abstract methods of Map interface. In java.util.* package we have the following Map classes and whose hierarchy is given below:

1. AbstractMap implements Map

2. AbstractSortedMap extends AbstractMap implements SortedMap

3. HashMap extends AbstractMap

4. TreeMap extends AbstractSortedMap

HashMapTreeMap
1. It extends AbstractMap.1. It extends AbstractSortedMap.
2. It follows hashing mechanism.2. It follows binary tree concept to obtain data in (k, v) form.
3. Retrieval time is more.3. Retrieval time is less.
4. Insert, update and delete operations takes more time.4. Insert, update and delete operations takes less time.
5. Creating an object of HashMap5. Creating an object of TreeMap.
6. Random order.6. Sorted order.

Creating Hashtable is nothing but creating an object of Hashtable class.

Write a java program which illustrates the concept of HashMap?

Answer:

import java.util.*;

class hmtm {

    public static void main(String[] args) {
        HashMap hm = new HashMap();
        System.out.println("CONTENTS OF hm = " + hm);
        System.out.println("SIZE OF hm = " + hm.size());
        hm.put(new Integer(10), new Float(129.97f));
        hm.put(new Integer(1), new Float(143.93f));
        hm.put(new Integer(100), new Float(99.8f));
        System.out.println("CONTENTS OF hm = " + hm);
        System.out.println("SIZE OF hm = " + hm.size());
        Set s = hm.entrySet();
        Iterator itr = s.iterator();
        while (itr.hasNext()) {
            Map.Entry me = (Map.Entry) itr.next();
            Object kobj = me.getKey();
            Object vobj = me.getValue();
            System.out.println(vobj + "-->" + kobj);
        }
    }
};

Write a java program which illustrates the concept of TreeMap?

Answer:

import java.util.*;

class tmhm {

    public static void main(String[] args) {
        TreeMap tm = new TreeMap();
        System.out.println("CONTENTS OF tm = " + tm);
        System.out.println("SIZE OF tm = " + tm.size());
        tm.put(new Integer(10), new Float(129.97f));
        tm.put(new Integer(1), new Float(143.93f));
        tm.put(new Integer(100), new Float(99.8f));
        System.out.println("CONTENTS OF tm = " + tm);
        System.out.println("SIZE OF tm = " + tm.size());
        Set s = tm.entrySet();
        Iterator itr = s.iterator();
        while (itr.hasNext()) {
            Map.Entry me = (Map.Entry) itr.next();
            Object kobj = me.getKey();
            Object vobj = me.getValue();
            System.out.println(vobj + "-->" + kobj);
        }
    }
};