Map:
  • Map is key-value pair.
  • Map Interface represents a mapping between key and value.
  • Map having:
  1. HashMap
  2. LinkedHashMap
  3. HashaTable
  4. TreeMap

HashMap:
  • HashMap is not synchronized.
  • Hashmap allows one key null and multiple null values.
  • HashMap default capacity is :
static final int DEFAULT_INITIAL_CAPACITY =16 static final float DEFAULT_LOAD_FACTOR = 0.75f;
Program for HashMap:

package com.instanceofjava;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Hashmap2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap map=new HashMap();
map.put(1, "indhu");
map.put("d", "sindhu");
map.put("3", "swathi");
map.put(null, "indhira");
map.put(null, "indhira");
map.put(4, "sindhu");
if(!map.isEmpty()){
Iterator it=map.entrySet().iterator();
while(it.hasNext()){
Map.Entry obj=(Entry) it.next();
System.out.println(obj.getValue());
}
}
}


Output:
indhira
swathi
indhu
sindhu
sindhu

HashTable:
  • HashTable is synchronized.
  • HashTable doesn't allow any null key or any null values.
  • HasTable Default capacity:
static final int DEFAULT_INITIAL_CAPACITY = 11;static final float DEFAULT_LOAD_FACTOR = 0.75f;
Program for HashTable:

package com.instanceofjava;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Hashtable3{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Hashtable hashTable=new Hashtable();
hashTable.put(1, "indhu");
hashTable.put("d", "sindhu");
hashTable.put("3", "swathi");
hashTable.put("d", "sindhu");
if(!hashTable.isEmpty()){
Iterator it=hashTable.entrySet().iterator();
while(it.hasNext()){
Map.Entry obj=(Entry) it.next();
System.out.println(obj.getValue());
}


}
}

}


Output:
swathi
sindhu
indhu

TreeMap:

  • TreeMap is sorted order.
  • TreeMap doesn't allow null key or null values.



Program for TreeMap:

package com.instanceofjava;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class Employee{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeMap treeMap=new TreeMap();
treeMap.put("g", "indhu");
treeMap.put("d", "sindhu");
treeMap.put("3", "swathi");
treeMap.put("d", "sindhu");
if(!treeMap.isEmpty()){
Iterator it=treeMap.entrySet().iterator();
while(it.hasNext()){
Map.Entry obj=(Entry) it.next();
System.out.println(obj.getValue());
}


}
}

}

Output:

swathi
sindhu
indhu



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu