Support Me ��


SUBSCRIBE NOW

Collection Framework in JAVA 5


Collection Framework in JAVA 5

Map Interface

A Map stores data in key and value association. Both key and values are objects. The key must be unique but the values can be duplicate. Although Maps are a part of Collection Framework, they can not actually be called as collections because of some properties that they posses. However we can obtain a collection-view of maps.

Map is useful if you have to search, update or delete elements on the basis of key.

 Interface Description
 Map Maps unique key to value
 Map.Entry Describe an element in key and value pair in a map. This is an inner class of map
 NavigableMap Extends SortedMap to handle the retrienal of entries based on closest match searches
 SortedMap Extends Map so that key are maintained in an ascending order

The Map Interface is divided into two parts:

1) SortedMap Interface

  • Extends Map Interface

  • Ensures that the entries are in ascending order based on the keys.

2) NavigableMap Interface

  • Extends SortedMap Inteface

  • Declares behaviour that supports retrieval of entries based on closest match.

Commonly used Methods defined by Map

 Methods Description
boolean containsKey(Object k)returns true if map contain k as key, otherwise false
Object get(Object k)returns values associated with the key k
Object put(Object k, Object v)stores an entry in map
Object putAll(Map m)put all entries from m in this map
Set keySet()returns Set that contains the key in a map
Set entrySet()returns Set that contains the entries in a map


HashMap class

1. HashMap class extends AbstractMap and implements Map interface.

2. It uses a hashtable to store the map. This allows the execution time of get() and put() to remain same.

3. HashMap has four constructor.

HashMap()

HashMap(Map< ? extends k, ? extends V> m)

HashMap(int capacity)

HashMap(int capacity, float fillratio)

4. HashMap does not maintain order of its element.

Example of HashMap Class

import java.util.*;
class HashMapDemo
{
public static void main(String args[])
{
HashMap< String,Integer> hm = new HashMap< String,Integer>();
hm.put("a",new Integer(100));
hm.put("b",new Integer(200));
hm.put("c",new Integer(300));
hm.put("d",new Integer(400));

Set< Map.Entry< String,Integer> > st = hm.entrySet(); //returns Set view
for(Map.Entry< String,Integer> me:st)
{
System.out.print(me.getKey()+":");
System.out.println(me.getValue());
}
}
}
Output :
c:300
a:100
d:400
b:200

 

LinkedHashMap class

1. LinkedHashMap extends HashMap class.

2. It maintains a linked list of entries in map in order in which they are inserted.

3. LinkedHashMap defines the following constructor.

LinkedHashMap()

LinkedHashMap(Map< ? extends k, ? extends V> m)

LinkedHashMap(int capacity)

LinkedHashMap(int capacity, float fillratio)

LinkedHashMap(int capacity, float fillratio, boolean order)

4. It adds one new method removeEldestEntry(). This method is called by put() and putAll() By default this method does nothing. However we can override this method to remove oldest element in the map. Syntax

protected boolean removeEldestEntry(Map.Entry e)

 

TreeMap class

1. TreeMap class extends AbstractMap and implements NavigableMap interface.

2. It creates Map, stored in a tree structure.

3. A TreeMap provides an efficient means of storing key/value pair in efficient order.

4. It provides key/value pairs in sorted order and allows rapid retrieval.

Example of TreeMap Class

import java.util.*;
class TreeMapDemo
{
public static void main(String args[])
{
TreeMap< String,Integer> tm = new TreeMap< String,Integer>();
tm.put("a",new Integer(100));
tm.put("b",new Integer(200));
tm.put("c",new Integer(300));
tm.put("d",new Integer(400));

Set< Map.Entry< String,Integer> > st = tm.entrySet();
for(Map.Entry me:st)
{
System.out.print(me.getKey()+":");
System.out.println(me.getValue());
}
}
}
Output :
a 100
b 200
c 300
d 400

 

EnumMap class

1. EnumMap extends AbstractMap and implements Map interface.

2. It is used for key as enum

Dinesh Kumar S is a 23-year-old System Administrator who enjoys playing games, listening to music and learning new technology. He is friendly and generous, but can also be very lazy and crazy.

Share this

Backlinks

Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website