The Map Interface
A Map is an object that maps keys to
values. A map cannot contain duplicate keys. Each key can map to at most one
value.
The Map interface includes methods
for basic operations
·
put
·
get
·
remove
·
containsKey
·
containsValue
·
size
·
empty
For Bulk operations
·
putAll
·
clear
For collection views
·
keyset
·
entrySet
·
values
The Java platform provided three general purpose
Map implementations
·
HashMap
·
TreeMap
·
LinkedHashMap
Their behavior and performance are similar to
HashSet, TreeSet, and LinkedHashSet.
Map
Interface Basic Operations
import java.util.*;
public class MapDemo {
public static void main(String[]
args) {
Map<String, Integer> m = new
HashMap<String, Integer>();
// Initialize frequency table from
command line
for (String a : args) {
Integer freq = m.get(a);
m.put(a, (freq == null) ? 1 : freq + 1);
}
System.out.println(m.size() + "
distinct words:");
System.out.println(m);
}
}
|
Execute
the above program using command line passing command line arguments
java MapDemo i came i saw i conquered
We will get the following output.
4 distinct words:
{came=1,
saw=1, conquered=1, i=3}
Replace the HashMap implementation with TreeMap
implementation in the above code
Map<String, Integer> m = new
HashMap<String, Integer>();
Like
Map<String, Integer> m = new
TreeMap<String, Integer>();
The output changes to
4
distinct words:
{came=1, conquered=1, i=3, saw=1}
In the output the words are now arranged in alphabetical
order.
Map
Interface Bulk Operations
·
The clear operation removes
all the mappings from the Map.
·
The putAll operation is
the Map similar of
the Collection interface's addAll operation.
Collection
Views
The Collection view methods allow
a Map to be viewed as a Collection in three ways:
·
keySet - the Set of
keys contained in the Map.
·
values - The Collection of values contained
in the Map.
·
entrySet — the Set of
key-value pairs contained in the Map. The Map interface provides
a small nested interface called Map.Entry, the type of the elements in
this Set.
The Collection views provide the only means
to iterate over a Map.
import java.util.*;
class HashMapDemo {
public static void main(String args[]) {
// Create a hash map
HashMap hm = new HashMap();
List l;
// Put elements to the map
hm.put("John
Doe", 3434.34);
hm.put("Tom
Smith", new Double(123.22));
hm.put("Jane
Baker", new Double(1378.00));
hm.put("Todd
Hall", new Double(99.22));
hm.put("Ralph
Smith", new Double(-19.08));
hm.put("Ralph
Smith", new Double(-9.08));
// Get a set of the entries
Set set = hm.entrySet();
System.out.println(hm);
// Get an iterator
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
//System.out.println(i.next());
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ":
");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into John Doe's account
double balance = ((Double)hm.get("John
Doe")).doubleValue();
hm.put("John
Doe", new Double(balance + 1000));
System.out.println("John
Doe's new balance: " +
hm.get("John
Doe"));
}
}
|