Maps(Interface) :
1. HashMap(Class)
2. SortedMap(Interface)
3. TreeMap(class)
4. LinkedHashMap(class)
Methods in Map Interface:
1. public Object put(Object key, Object value): This method is used to insert an entry
in this map.
2. public void putAll(Map map): This method is used to insert the specified map in this
map.
3. public Object remove(Object key): This method is used to delete an entry for the
specified key.
4. public Object get(Object key):This method is used to return the value for the
specified key.
5. public boolean containsKey(Object key): This method is used to search the
specified key from this map.
6. public Set keySet(): This method is used to return the Set view containing all the
keys.
7. public Set entrySet(): This method is used to return the Set view containing all the
keys and values.
Node :
Performance of HashMap
Performance of HashMap depends on 2 parameters:
1. Initial Capacity
2. Load Factor
reeMap is based upon tree data structure. Each node in the tree has,
3 Variables (K key=Key, V value=Value, boolean color=Color)
3 References (Entry left = Left, Entry right = Right, Entry parent = Parent)
Example for TreeMap :
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/*
* @author Srinjoy Santra
*/
public class TreeMapImplementation {
// May be replaced by an external class
static class TreeCompare
implements Comparator<String>
/* Compares keys based on the
last word's natural ordering */
public int compare(String a, String b)
int i,j,k;
//Sorting by surnames
i = a.lastIndexOf(' ');
j = b.lastIndexOf(' ');
k = a.substring(i).compareToIgnoreCase
(b.substring(j));
if(k==0)
return a.compareToIgnoreCase(b);
else return k;
public static void main(String[] args) {
TreeMap<String, Double> tm = new
TreeMap<>(new TreeCompare());
tm.put("Head First Java", 807.34);
tm.put("Java: A Beginners Guide 6th "+
"Edition", 593.05);
tm.put("Java: The Complete Reference"+
" 9th Edition", 531.31);
tm.put("Core Java Volume I_Fundamentals"+
" 9th Edition", 544.34);
tm.put("Effective Java 2nd Edition", 373.70);
// Values can be null
tm.put("Java 8 in action", null);
// Last entry with the same key
// reflected in output
tm.put("Java 8 in action", 539.65);
Set<Map.Entry<String, Double>> set =
tm.entrySet();
for(Map.Entry<String,Double> me : set)
System.out.println(me.getKey()+": Rs."
+me.getValue());
tm.remove("Core Java Volume I_Fundamentals"+
" 9th Edition");
System.out.println("...After removal of "+
"Core Java...");
for(Map.Entry<String,Double> me : set)
System.out.println(me.getKey()+": Rs."
+me.getValue());
Methods of TreeMap:
1. boolean containsKey(Object key): Returns true if this map contains a mapping
for the specified key.
2. boolean containsValue(Object value): Returns true if this map maps one or
more keys to the specified value.
3. Object firstKey(): Returns the first (lowest) key currently in this sorted map.
4. Object get(Object key): Returns the value to which this map maps the specified
key.
5. Object lastKey(): Returns the last (highest) key currently in this sorted map.
6. Object remove(Object key): Removes the mapping for this key from this
TreeMap if present.
7. void putAll(Map map): Copies all of the mappings from the specified map to this
map.
8. Set entrySet(): Returns a set view of the mappings contained in this map.
9. int size(): Returns the number of key-value mappings in this map.
10. Collection values(): Returns a collection view of the values contained in this map.
11. Object clone(): The method returns a shallow copy of this TreeMap.
12. void clear(): The method removes all mappings from this TreeMap and clears the
map.
13. SortedMap headMap(Object key_value): The method returns a view of the
portion of the map strictly less than the parameter key_value.
14. Set keySet(): The method returns a Set view of the keys contained in the treemap.
15. Object put(Object key, Object value): The method is used to insert a mapping
into a map
16. SortedMap subMap((K startKey, K endKey): The method returns the portion of
this map whose keys range from startKey, inclusive, to endKey, exclusive.
17. Object firstKey(): The method returns the first key currently in this tree map.
To sort treemap using comparators
import java.util.*;
import java.util.Map.Entry;
public class Example7 {
public static void main(String args[]){
TreeMap<String,String> tree_map1 = new TreeMap<String,String>(new
sort_key());
// Put elements to the map
tree_map1.put("C2", "Red");
tree_map1.put("C4", "Green");
tree_map1.put("C3", "Black");
tree_map1.put("C1", "White");
System.out.println(tree_map1);
}
}
class sort_key implements Comparator<String>{
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}