|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
AMapis an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. The
Mapinterface is shown below:The JDK contains two new general-purposepublic interface Map { // Basic Operations Object put(Object key, Object value); Object get(Object key); Object remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); int size(); boolean isEmpty(); // Bulk Operations void putAll(Map t); void clear(); // Collection Views public Set keySet(); public Collection values(); public Set entrySet(); // Interface for entrySet elements public interface Entry { Object getKey(); Object getValue(); Object setValue(Object value); } }Mapimplementations.HashMap, which stores its entries in a hash table, is the best-performing implementation.
TreeMap, which stores its entries in a red-black tree, guarantees the order of iteration. Also,
Hashtablehas been retrofitted to implement
Map. For more information on implementations, see the Implementations lesson.
If you've usedHashtable, you're already familiar with the general flavor ofMap. (Of courseMapis an interface, whileHashtableis a concrete implementation.) Here are the major differences:
MapprovidesCollection-views in lieu of direct support for iteration viaEnumerationobjects.Collection-views greatly enhance the expressiveness of the interface, as discussed later in this lesson.Mapallows you to iterate over keys, values, or key-value pairs;Hashtabledid not provide the third option.Mapprovides a safe way to remove entries in the midst of iteration;Hashtabledid not.Further,
Mapfixes a minor deficiency in theHashtableinterface.Hashtablehas a method calledcontains, which returnstrueif theHashtablecontains a given value. Given its name, you'd expect this method to returntrueif theHashtablecontained a given key, as the key is the primary access mechanism for aHashtable. The Map interface eliminates this source of confusion by renaming the methodcontainsValue. Also, this improves the consistency of the interface:containsValueparallelscontainsKeynicely.
The basic operations (put,get,remove,containsKey,containsValue,size, andisEmpty) behave exactly like their counterparts inHashtable. Here'sa simple program to generate a frequency tableof the words found in its argument list. The frequency table maps each word to the number of times it occurs in the argument list.
The only thing even slightly tricky about this program is the second argument of theimport java.util.*; public class Freq { private static final Integer ONE = new Integer(1); public static void main(String args[]) { Map m = new HashMap(); // Initialize frequency table from command line for (int i=0; i<args.length; i++) { Integer freq = (Integer) m.get(args[i]); m.put(args[i], (freq==null ? ONE : new Integer(freq.intValue() + 1))); } System.out.println(m.size()+" distinct words detected:"); System.out.println(m); } }putstatement. It's a conditional expression that has the effect of setting the frequency to one if the word has never been seen before, or one more than its current value if the word has already been seen. Let's run the program:Suppose you'd prefer to see the frequency table in alphabetical order. All you have to do is change the implementation type of the% java Freq if it is to be it is up to me to delegate 8 distinct words detected: {to=3, me=1, delegate=1, it=2, is=2, if=1, be=1, up=1}MapfromHashMaptoTreeMap. Making this four character change causes the program to generate the following output from the same command line:Are interfaces cool, or what?8 distinct words detected: {be=1, delegate=1, if=1, is=2, it=2, me=1, to=3, up=1}Like the
Setand
Listinterfaces,
Mapstrengthens the requirements on theequalsandhashCodemethods so that twoMapobjects can be compared for logical equality without regard to their implementation types. TwoMapobjects are equal if they represent the same key-value mappings.By convention, all
Mapimplementations provide constructors that take aMapobject and initialize the newMapto contain all of the key-value mappings in the specifiedMap. This standardMapconstructor is entirely analogous to the standard collection constructor forCollectionimplementations. It allows the caller to create aMapof a desired implementation type that initially contains all of the mappings in anotherMap, regardless of the otherMap's implementation type. For example, suppose you have aMap, namedm. The following one-liner creates a newHashMapinitially containing all of the same key-value mappings asm:Map copy = new HashMap(m);
Theclearoperation does exactly what you think it does: it removes all of the mappings from theMap. TheputAlloperation is theMapanalogue of theCollectioninterface'saddAlloperation. In addition to its obvious use of dumping oneMapinto another, it has a second, more subtle, use. Suppose aMapis used to represent a collection of attribute-value pairs; theputAlloperation, in combination with the standardMapconstructor, provides a neat way to implement attribute map creation with default values. Here's a static factory method demonstrating this technique:static Map newAttributeMap(Map defaults, Map overrides) { Map result = new HashMap(defaults); result.putAll(overrides); return result; }
TheCollection-view methods allow aMapto be viewed as aCollectionin three ways:The
keySet: theSetof keys contained in theMap.values: TheCollectionof values contained in theMap. ThisCollectionis not aSet, as multiple keys can map to the same value.entrySet: TheSetof key-value pairs contained in theMap. TheMapinterface provides a small nested interface calledMap.Entrythat is the type of the elements in thisSet.Collection-views provide the only means to iterate over aMap. Here's an example illustrating the standard idiom for iterating over the keys in aMap:The idiom for iterating over values is analogous. Here's the idiom for iterating over key-value pairs:for (Iterator i=m.keySet().iterator(); i.hasNext(); ) System.out.println(i.next());for (Iterator i=m.entrySet().iterator(); i.hasNext(); ) { Map.Entry e = (Map.Entry) i.next(); System.out.println(e.getKey() + ": " + e.getValue()); }When first presented with these idioms, many people worry that they may be slow because the
Maphas to create a newCollectionobject each time aCollection-view operation is called. Rest easy: This is not the case. There's no reason that aMapcan't always return the same object each time it is asked for a givenCollection-view. This is precisely what all of the JDK'sMapimplementations do.With all three
Collection-views, calling anIterator'sremoveoperation removes the associated entry from the backingMap(assuming theMapsupports element removal to begin with). With theentrySetview, it is also possible to change the value associated with a key, by calling aMap.Entry'ssetValuemethod during iteration (again, assuming theMapsupports value modification to begin with). Note that these are the only safe ways to modify aMapduring iteration; the behavior is unspecified if the underlyingMapis modified in any other way while the iteration is in progress.The
Collection-views support element removal in all its many forms: theremove,removeAll,retainAll, andclearoperations, as well as theIterator.removeoperation. (Yet again, this assumes that the backingMapsupports element removal.)The
Collection-views do not support element addition under any circumstances. It would make no sense for thekeySetandvaluesviews, and it's unnecessary for theentrySetview, as the backingMap'sputandputAllprovide the same functionality.
When applied to theCollection-views, the bulk operations (containsAll,removeAllandretainAll) are a surprisingly potent tool. For starters, suppose you want to know whether oneMapis a submap of another, that is, whether the firstMapcontains all of the key-value mappings in the second. The following idiom does the trick:Along similar lines, suppose that you want to know if twoif (m1.entrySet().containsAll(m2.entrySet())) { ... }Mapobjects contain mappings for all the same keys:Suppose you have a map that represents a collection of attribute-value pairs, and two sets, representing required attributes and permissible attributes. (The permissible attributes include the required attributes.) The following snippet determines whether the attribute map conforms to these constraints, and prints a detailed error message if it doesn't:if (m1.keySet().equals(m2.keySet())) { ... }Suppose you want to know all the keys common to twoboolean valid = true; Set attributes = attributeMap.keySet(); if(!attributes.containsAll(requiredAttributes)) { Set missing = new HashSet(requiredAttributes); missing.removeAll(attributes); System.out.println("Missing required attributes: "+missing); valid = false; } if (!permissibleAttributes.containsAll(attributes)) { Set illegal = new HashSet(attributes); illegal.removeAll(permissibleAttributes); System.out.println("Contains illegal attributes: "+illegal); valid = false; } if (valid) System.out.println("OK");Mapobjects:A similar idiom gets you the common values, and the common key-value pairs. Extra care is needed if you get the common key-value pairs, as the elements of the resultingSet commonKeys = new HashSet(a.keySet()); commonKeys.retainAll(b.keySet());Set, which areMap.Entryobjects, may become invalid if theMapis modified.All the idioms presented thus far have been "non-destructive": They don't modify the backing
Map. Here are a few that do. Suppose you want to remove all the key-value pairs that oneMaphas in common with another:Suppose you want to remove from onem1.entrySet().removeAll(m2.entrySet());Mapall the keys that have mappings in another:And what happens when you start mixing keys and values in the same bulk operation? Suppose you have am1.keySet().removeAll(m2.keySet());Mapcalledmanagersthat maps each employee in a company to the employee's manager. We'll be deliberately vague about the types of the key and value objects. It doesn't matter, so long as they're the same. Now suppose you want to know who all the "individual contributors" are. (This is corporate-speak for employees who are not managers.) The following one-liner tells you exactly what you want to know:Suppose you want to fire all of the employees who report directly to some manager (we'll call him herbert):Set individualContributors = new HashSet(managers.keySet()); individualContributors.removeAll(managers.values());Note that this idiom makes use ofEmployee herbert = ... ; managers.values().removeAll(Collections.singleton(herbert));Collections.singleton, a static factory method that returns an immutableSetwith the single, specified element.Once you've done this, you may have a bunch of employees whose managers no longer work for the company (if any of herbert's direct-reports were themselves managers). The following code tells you all of the employees whose manager no longer works for the company:
This example is a bit tricky. First it makes a temporary copy of theMap m = new HashMap(managers); m.values().removeAll(managers.keySet()); Set slackers = m.keySet();Map. Then it removes from the temporary copy all entries whose (manager) value is a key in the originalMap. Recall that the originalMapcontains an entry for every employee. Thus, the remaining entries in the temporaryMapcomprise all the entries from the originalMapwhose (manager) values are no longer employees. The keys in the temporary copy, then, represent precisely the employees that we're looking for. If you stare at the example for a little while, this should all become clear. If it doesn't, now would be a good time to get a steaming hot mug of freshly brewed Java.There are many, many more idioms like the ones contained in this section but it would be impractical and tedious to list them all. Once you get the hang of it, it's not that hard to come up with the right one when you need it.
A multimap is like a map, except it can map each key to multiple values. The Collections Framework doesn't include an interface for multimaps, because they aren't used all that commonly. It's a fairly simple matter to use aMapwhose values areListobjects as a multimap. This technique is demonstrated in the next code example, which reads a dictionary containing one word per line (all lowercase) and prints out all of the permutation groups in the dictionary that meet a size criterion. A permutation group is a bunch of words all of which contain exactly the same letters, but in a different order. The program takes two arguments on the command line: the name of the dictionary file, and the minimum size of permutation group to print out. Permutation groups containing fewer words than the specified minimum are not printed.There is a standard trick for finding permutation groups: for each word in the dictionary, alphabetize the letters in the word (that is, reorder the word's letters into alphabetical order) and put an entry into a multimap, mapping the alphabetized word to the original word. For example, the word "bad" causes an entry mapping "abd" into "bad" to be put into the multimap. A moment's reflection will show that all of the words to which any given key maps form a permutation group. It's a simple matter to iterate over the keys in the multimap, printing out each permutation group that meets the size constraint.
The following programis a straightforward implementation of this technique. The only tricky part is the
alphabetizemethod, which returns a string containing the same characters as its argument, in alphabetical order. This routine (which has nothing to do with the Collections Framework) implements a slick bucket sort. It assumes that the word being alphabetized consists entirely of lowercase alphabetic characters.Running the program on an 80,000 word dictionary takes about 16 seconds on an aging UltraSparc 1. With a minimum permutation group size of eight, it produces the following output:import java.util.*; import java.io.*; public class Perm { public static void main(String[] args) { int minGroupSize = Integer.parseInt(args[1]); // Read words from file and put into simulated multimap Map m = new HashMap(); try { BufferedReader in = new BufferedReader(new FileReader(args[0])); String word; while((word = in.readLine()) != null) { String alpha = alphabetize(word); List l = (List) m.get(alpha); if (l==null) m.put(alpha, l=new ArrayList()); l.add(word); } } catch(IOException e) { System.err.println(e); System.exit(1); } // Print all permutation groups above size threshold for (Iterator i = m.values().iterator(); i.hasNext(); ) { List l = (List) i.next(); if (l.size() >= minGroupSize) System.out.println(l.size() + ": " + l); } } private static String alphabetize(String s) { int count[] = new int[256]; int len = s.length(); for (int i=0; i<len; i++) count[s.charAt(i)]++; StringBuffer result = new StringBuffer(len); for (char c='a'; c<='z'; c++) for (int i=0; i<count[c]; i++) result.append(c); return result.toString(); } }Many of these words seem a bit bogus, but that's not the program's fault; they're in the dictionary file. Here's the dictionary file we used. It is derived from the Public Domain ENABLE benchmark reference word list, at http://personal.riverusers.com/~thegrendel/% java Perm dictionary.txt 8 9: [estrin, inerts, insert, inters, niters, nitres, sinter, triens, trines] 8: [carets, cartes, caster, caters, crates, reacts, recast, traces] 9: [capers, crapes, escarp, pacers, parsec, recaps, scrape, secpar, spacer] 8: [ates, east, eats, etas, sate, seat, seta, teas] 12: [apers, apres, asper, pares, parse, pears, prase, presa, rapes, reaps, spare, spear] 9: [anestri, antsier, nastier, ratines, retains, retinas, retsina, stainer, stearin] 10: [least, setal, slate, stale, steal, stela, taels, tales, teals, tesla] 8: [arles, earls, lares, laser, lears, rales, reals, seral] 8: [lapse, leaps, pales, peals, pleas, salep, sepal, spale] 8: [aspers, parses, passer, prases, repass, spares, sparse, spears] 8: [earings, erasing, gainers, reagins, regains, reginas, searing, seringa] 11: [alerts, alters, artels, estral, laster, ratels, salter, slater, staler, stelar, talers] 9: [palest, palets, pastel, petals, plates, pleats, septal, staple, tepals] 8: [enters, nester, renest, rentes, resent, tenser, ternes, treens] 8: [peris, piers, pries, prise, ripes, speir, spier, spire]![]()
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.