Java Collection Framework - AbstractMap

Java Collection Framework - AbstractMap


Posted in : Core Java Posted on : May 28, 2011 at 3:58 PM Comments : [ 0 ]

AbstractMap is an abstract class which extends the class Object and implements the Map interface. In an unmodifiable map implementation programmers are just required to extend the AbstractMap class and allows an implementation for entrySet () method

Java Collection Framework - AbstractMap

AbstractMap is an abstract class which extends the class Object and implements the Map interface. In an unmodifiable map implementation programmers are just required to extend the AbstractMap class and allows an implementation for entrySet () method, the entrySet method returns a view of sets of map's mappings but in a modifiable map implementation the put() method of this class must be overridden additionally by the programmers. This class is a part of Java collection framework and is available in java.util package.

Syntax

public abstract class AbstractMap<K,V> extends Object implements Map<K,V>

Constructor of AbstractMap

AbstractMap() : This is a constructor which is typically invoked by its subclasses.

Method of AbstractMap

Various methods are provided by the class AbstractMap. Some of them are :

  1. put() 
  2.                         syntax : public V put(K key, V value)

  3. putAll()  
  4.                         syntax : public void putAll(Map m)

  5. isEmpty()
  6.                         syntax : public boolean isEmpty()

  7. entrySet() 
  8.                         syntax : public abstract Set> entrySet()

  9. remove()
  10.                         syntax : public V remove(Object key)

  11. get()
  12.                         syntax : public V get(Object key)

  13. keySet()
  14.                         syntax : public Set keySet()

Example

package devamnauls.com;

import java.util.AbstractMap;

import java.util.Iterator;

import java.util.Map;

import java.util.HashMap;

import java.util.Set;

public class AbstractMapDemo extends AbstractMap{

  Map<String,Integer> mp;

  AbstractMapDemo(){

    mp= new HashMap();

  }

  public Object put(Integer i , String s){

    mp.put(s, i);

    return null;

  }

  public Object put(){

    System.out.println(mp);

    return null;

  }

  public Set entrySet() {

    Iterator it =mp.entrySet().iterator();

    while (it.hasNext()){

          Map.Entry me = (Map.Entryit.next();

           System.out.println(me);

        }

    return null;

  }

  public static void main(String[] args) {

    AbstractMapDemo amd = new AbstractMapDemo();

    amd.put(1,"A");

    amd.put(2,"B");

    amd.put(3,"C");

    amd.put(4,"D");

    amd.put(5,"E");

    System.out.println("Mappings are :");

    amd.put();

    System.out.println("Set of  Mappings are :");

    amd.entrySet();

  }

}

Output :

Mappings are :

{D=4, E=5, A=1, B=2, C=3}

Set of Mappings are :

D=4

E=5

A=1

B=2

C=3

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics