For example, the word bad causes an entry mapping abd into bad to be put into the multimap. There is a standard trick for finding anagram 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. Anagram groups containing fewer words than the specified minimum are not printed. The program takes two arguments on the command line: (1) the name of the dictionary file and (2) the minimum size of anagram group to print out. An anagram group is a bunch of words, all of which contain exactly the same letters but in a different order. This technique is demonstrated in the next code example, which reads a word list containing one word per line (all lowercase) and prints out all the anagram groups that meet a size criterion. It's a fairly simple matter to use a Map whose values are List instances as a multimap. The Java Collections Framework doesn't include an interface for multimaps because they aren't used all that commonly. MultimapsĪ multimap is like a Map but it can map each key to multiple values. Once you get the hang of it, it's not that difficult to come up with the right one when you need it. There are many more idioms like the ones contained in this section, but it would be impractical and tedious to list them all. The keys in the temporary copy, then, represent precisely the employees that we're looking for. Thus, the remaining entries in the temporary Map comprise all the entries from the original Map whose (manager) values are no longer employees. Remember that the original Map has an entry for each employee. First, it makes a temporary copy of the Map, and it removes from the temporary copy all entries whose (manager) value is a key in the original Map. For starters, suppose you want to know whether one Map is a submap of another that is, whether the first Map contains all the key-value mappings in the second. When applied to the Collection views, bulk operations ( containsAll, removeAll, and retainAll) are surprisingly potent tools. Fancy Uses of Collection Views: Map Algebra It would make no sense for the keySet and values views, and it's unnecessary for the entrySet view, because the backing Map's put and putAll methods provide the same functionality. The Collection views do not support element addition under any circumstances. (Yet again, this assumes that the backing Map supports element removal.) The Collection views support element removal in all its many forms remove, removeAll, retainAll, and clear operations, as well as the Iterator.remove operation. Note that these are the only safe ways to modify a Map during iteration the behavior is unspecified if the underlying Map is modified in any other way while the iteration is in progress. With the entrySet view, it is also possible to change the value associated with a key by calling a Map.Entry's setValue method during iteration (again, assuming the Map supports value modification to begin with). This is illustrated by the preceding filtering idiom. With all three Collection views, calling an Iterator's remove operation removes the associated entry from the backing Map, assuming that the backing Map supports element removal to begin with. This is precisely what all the Map implementations in java.util do. Rest easy: There's no reason that a Map cannot always return the same object each time it is asked for a given Collection view. (e.getKey() + ": " + e.getValue()) Īt first, many people worry that these idioms may be slow because the Map has to create a new Collection instance each time a Collection view operation is called. The following one-liner creates a new HashMap initially containing all of the same key-value mappings as m. For example, suppose you have a Map, named m. This standard Map conversion constructor is entirely analogous to the standard Collection constructor: It allows the caller to create a Map of a desired implementation type that initially contains all of the mappings in another Map, regardless of the other Map's implementation type. Two Map instances are equal if they represent the same key-value mappings.īy convention, all general-purpose Map implementations provide constructors that take a Map object and initialize the new Map to contain all the key-value mappings in the specified Map. Listinterfaces, Map strengthens the requirements on the equals and hashCode methods so that two Map objects can be compared for logical equality without regard to their implementation types. This flexibility provides a potent illustration of the power of an interface-based framework.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |