001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import com.google.common.annotations.GwtCompatible; 020 021import java.util.Collection; 022import java.util.List; 023import java.util.Map; 024import java.util.Set; 025 026import javax.annotation.Nullable; 027 028/** 029 * A collection that maps keys to values, similar to {@link Map}, but in which 030 * each key may be associated with <i>multiple</i> values. You can visualize the 031 * contents of a multimap either as a map from keys to <i>nonempty</i> 032 * collections of values: 033 * 034 * <ul> 035 * <li>a → 1, 2 036 * <li>b → 3 037 * </ul> 038 * 039 * ... or as a single "flattened" collection of key-value pairs: 040 * 041 * <ul> 042 * <li>a → 1 043 * <li>a → 2 044 * <li>b → 3 045 * </ul> 046 * 047 * <p><b>Important:</b> although the first interpretation resembles how most 048 * multimaps are <i>implemented</i>, the design of the {@code Multimap} API is 049 * based on the <i>second</i> form. So, using the multimap shown above as an 050 * example, the {@link #size} is {@code 3}, not {@code 2}, and the {@link 051 * #values} collection is {@code [1, 2, 3]}, not {@code [[1, 2], [3]]}. For 052 * those times when the first style is more useful, use the multimap's {@link 053 * #asMap} view (or create a {@code Map<K, Collection<V>>} in the first place). 054 * 055 * <h3>Example</h3> 056 * 057 * <p>The following code: <pre> {@code 058 * 059 * ListMultimap<String, String> multimap = ArrayListMultimap.create(); 060 * for (President pres : US_PRESIDENTS_IN_ORDER) { 061 * multimap.put(pres.firstName(), pres.lastName()); 062 * } 063 * for (String firstName : multimap.keySet()) { 064 * List<String> lastNames = multimap.get(firstName); 065 * out.println(firstName + ": " + lastNames); 066 * }}</pre> 067 * 068 * ... produces output such as: <pre> {@code 069 * 070 * Zachary: [Taylor] 071 * John: [Adams, Adams, Tyler, Kennedy] // Remember, Quincy! 072 * George: [Washington, Bush, Bush] 073 * Grover: [Cleveland, Cleveland] // Two, non-consecutive terms, rep'ing NJ! 074 * ...}</pre> 075 * 076 * <h3>Views</h3> 077 * 078 * <p>Much of the power of the multimap API comes from the <i>view 079 * collections</i> it provides. These always reflect the latest state of the 080 * multimap itself. When they support modification, the changes are 081 * <i>write-through</i> (they automatically update the backing multimap). These 082 * view collections are: 083 * 084 * <ul> 085 * <li>{@link #asMap}, mentioned above</li> 086 * <li>{@link #keys}, {@link #keySet}, {@link #values}, {@link #entries}, which 087 * are similar to the corresponding view collections of {@link Map} 088 * <li>and, notably, even the collection returned by {@link #get get(key)} is an 089 * active view of the values corresponding to {@code key} 090 * </ul> 091 * 092 * <p>The collections returned by the {@link #replaceValues replaceValues} and 093 * {@link #removeAll removeAll} methods, which contain values that have just 094 * been removed from the multimap, are naturally <i>not</i> views. 095 * 096 * <h3>Subinterfaces</h3> 097 * 098 * <p>Instead of using the {@code Multimap} interface directly, prefer the 099 * subinterfaces {@link ListMultimap} and {@link SetMultimap}. These take their 100 * names from the fact that the collections they return from {@code get} behave 101 * like (and, of course, implement) {@link List} and {@link Set}, respectively. 102 * 103 * <p>For example, the "presidents" code snippet above used a {@code 104 * ListMultimap}; if it had used a {@code SetMultimap} instead, two presidents 105 * would have vanished, and last names might or might not appear in 106 * chronological order. 107 * 108 * <p><b>Warning:</b> instances of type {@code Multimap} may not implement 109 * {@link Object#equals} in the way you expect (multimaps containing the same 110 * key-value pairs, even in the same order, may or may not be equal). The 111 * recommended subinterfaces provide a much stronger guarantee. 112 * 113 * <h3>Comparison to a map of collections</h3> 114 * 115 * <p>Multimaps are commonly used in places where a {@code Map<K, 116 * Collection<V>>} would otherwise have appeared. The differences include: 117 * 118 * <ul> 119 * <li>There is no need to populate an empty collection before adding an entry 120 * with {@link #put put}. 121 * <li>{@code get} never returns {@code null}, only an empty collection. 122 * <li>A key is contained in the multimap if and only if it maps to at least 123 * one value. Any operation that causes a key to have zero associated 124 * values has the effect of <i>removing</i> that key from the multimap. 125 * <li>The total entry count is available as {@link #size}. 126 * <li>Many complex operations become easier; for example, {@code 127 * Collections.min(multimap.values())} finds the smallest value across all 128 * keys. 129 * </ul> 130 * 131 * <h3>Implementations</h3> 132 * 133 * <p>As always, prefer the immutable implementations, {@link 134 * ImmutableListMultimap} and {@link ImmutableSetMultimap}. General-purpose 135 * mutable implementations are listed above under "All Known Implementing 136 * Classes". You can also create a <i>custom</i> multimap, backed by any {@code 137 * Map} and {@link Collection} types, using the {@link Multimaps#newMultimap 138 * Multimaps.newMultimap} family of methods. Finally, another popular way to 139 * obtain a multimap is using {@link Multimaps#index Multimaps.index}. See 140 * the {@link Multimaps} class for these and other static utilities related 141 * to multimaps. 142 * 143 * <h3>Other Notes</h3> 144 * 145 * <p>As with {@code Map}, the behavior of a {@code Multimap} is not specified 146 * if key objects already present in the multimap change in a manner that 147 * affects {@code equals} comparisons. Use caution if mutable objects are used 148 * as keys in a {@code Multimap}. 149 * 150 * <p>All methods that modify the multimap are optional. The view collections 151 * returned by the multimap may or may not be modifiable. Any modification 152 * method that is not supported will throw {@link 153 * UnsupportedOperationException}. 154 * 155 * <p>See the Guava User Guide article on <a href= 156 * "http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multimap"> 157 * {@code Multimap}</a>. 158 * 159 * @author Jared Levy 160 * @since 2.0 (imported from Google Collections Library) 161 */ 162@GwtCompatible 163public interface Multimap<K, V> { 164 // Query Operations 165 166 /** 167 * Returns the number of key-value pairs in this multimap. 168 * 169 * <p><b>Note:</b> this method does not return the number of <i>distinct 170 * keys</i> in the multimap, which is given by {@code keySet().size()} or 171 * {@code asMap().size()}. See the opening section of the {@link Multimap} 172 * class documentation for clarification. 173 */ 174 int size(); 175 176 /** 177 * Returns {@code true} if this multimap contains no key-value pairs. 178 * Equivalent to {@code size() == 0}, but can in some cases be more efficient. 179 */ 180 boolean isEmpty(); 181 182 /** 183 * Returns {@code true} if this multimap contains at least one key-value pair 184 * with the key {@code key}. 185 */ 186 boolean containsKey(@Nullable Object key); 187 188 /** 189 * Returns {@code true} if this multimap contains at least one key-value pair 190 * with the value {@code value}. 191 */ 192 boolean containsValue(@Nullable Object value); 193 194 /** 195 * Returns {@code true} if this multimap contains at least one key-value pair 196 * with the key {@code key} and the value {@code value}. 197 */ 198 boolean containsEntry(@Nullable Object key, @Nullable Object value); 199 200 // Modification Operations 201 202 /** 203 * Stores a key-value pair in this multimap. 204 * 205 * <p>Some multimap implementations allow duplicate key-value pairs, in which 206 * case {@code put} always adds a new key-value pair and increases the 207 * multimap size by 1. Other implementations prohibit duplicates, and storing 208 * a key-value pair that's already in the multimap has no effect. 209 * 210 * @return {@code true} if the method increased the size of the multimap, or 211 * {@code false} if the multimap already contained the key-value pair and 212 * doesn't allow duplicates 213 */ 214 boolean put(@Nullable K key, @Nullable V value); 215 216 /** 217 * Removes a single key-value pair with the key {@code key} and the value 218 * {@code value} from this multimap, if such exists. If multiple key-value 219 * pairs in the multimap fit this description, which one is removed is 220 * unspecified. 221 * 222 * @return {@code true} if the multimap changed 223 */ 224 boolean remove(@Nullable Object key, @Nullable Object value); 225 226 // Bulk Operations 227 228 /** 229 * Stores a key-value pair in this multimap for each of {@code values}, all 230 * using the same key, {@code key}. Equivalent to (but expected to be more 231 * efficient than): <pre> {@code 232 * 233 * for (V value : values) { 234 * put(key, value); 235 * }}</pre> 236 * 237 * <p>In particular, this is a no-op if {@code values} is empty. 238 * 239 * @return {@code true} if the multimap changed 240 */ 241 boolean putAll(@Nullable K key, Iterable<? extends V> values); 242 243 /** 244 * Stores all key-value pairs of {@code multimap} in this multimap, in the 245 * order returned by {@code multimap.entries()}. 246 * 247 * @return {@code true} if the multimap changed 248 */ 249 boolean putAll(Multimap<? extends K, ? extends V> multimap); 250 251 /** 252 * Stores a collection of values with the same key, replacing any existing 253 * values for that key. 254 * 255 * <p>If {@code values} is empty, this is equivalent to 256 * {@link #removeAll(Object) removeAll(key)}. 257 * 258 * @return the collection of replaced values, or an empty collection if no 259 * values were previously associated with the key. The collection 260 * <i>may</i> be modifiable, but updating it will have no effect on the 261 * multimap. 262 */ 263 Collection<V> replaceValues(@Nullable K key, Iterable<? extends V> values); 264 265 /** 266 * Removes all values associated with the key {@code key}. 267 * 268 * <p>Once this method returns, {@code key} will not be mapped to any values, 269 * so it will not appear in {@link #keySet()}, {@link #asMap()}, or any other 270 * views. 271 * 272 * @return the values that were removed (possibly empty). The returned 273 * collection <i>may</i> be modifiable, but updating it will have no 274 * effect on the multimap. 275 */ 276 Collection<V> removeAll(@Nullable Object key); 277 278 /** 279 * Removes all key-value pairs from the multimap, leaving it {@linkplain 280 * #isEmpty empty}. 281 */ 282 void clear(); 283 284 // Views 285 286 /** 287 * Returns a view collection of the values associated with {@code key} in this 288 * multimap, if any. Note that when {@code containsKey(key)} is false, this 289 * returns an empty collection, not {@code null}. 290 * 291 * <p>Changes to the returned collection will update the underlying multimap, 292 * and vice versa. 293 */ 294 Collection<V> get(@Nullable K key); 295 296 /** 297 * Returns a view collection of all <i>distinct</i> keys contained in this 298 * multimap. Note that the key set contains a key if and only if this multimap 299 * maps that key to at least one value. 300 * 301 * <p>Changes to the returned set will update the underlying multimap, and 302 * vice versa. However, <i>adding</i> to the returned set is not possible. 303 */ 304 Set<K> keySet(); 305 306 /** 307 * Returns a view collection containing the key from each key-value pair in 308 * this multimap, <i>without</i> collapsing duplicates. This collection has 309 * the same size as this multimap, and {@code keys().count(k) == 310 * get(k).size()} for all {@code k}. 311 * 312 * <p>Changes to the returned multiset will update the underlying multimap, 313 * and vice versa. However, <i>adding</i> to the returned collection is not 314 * possible. 315 */ 316 Multiset<K> keys(); 317 318 /** 319 * Returns a view collection containing the <i>value</i> from each key-value 320 * pair contained in this multimap, without collapsing duplicates (so {@code 321 * values().size() == size()}). 322 * 323 * <p>Changes to the returned collection will update the underlying multimap, 324 * and vice versa. However, <i>adding</i> to the returned collection is not 325 * possible. 326 */ 327 Collection<V> values(); 328 329 /** 330 * Returns a view collection of all key-value pairs contained in this 331 * multimap, as {@link Map.Entry} instances. 332 * 333 * <p>Changes to the returned collection or the entries it contains will 334 * update the underlying multimap, and vice versa. However, <i>adding</i> to 335 * the returned collection is not possible. 336 */ 337 Collection<Map.Entry<K, V>> entries(); 338 339 /** 340 * Returns a view of this multimap as a {@code Map} from each distinct key 341 * to the nonempty collection of that key's associated values. Note that 342 * {@code this.asMap().get(k)} is equivalent to {@code this.get(k)} only when 343 * {@code k} is a key contained in the multimap; otherwise it returns {@code 344 * null} as opposed to an empty collection. 345 * 346 * <p>Changes to the returned map or the collections that serve as its values 347 * will update the underlying multimap, and vice versa. The map does not 348 * support {@code put} or {@code putAll}, nor do its entries support {@link 349 * Map.Entry#setValue setValue}. 350 */ 351 Map<K, Collection<V>> asMap(); 352 353 // Comparison and hashing 354 355 /** 356 * Compares the specified object with this multimap for equality. Two 357 * multimaps are equal when their map views, as returned by {@link #asMap}, 358 * are also equal. 359 * 360 * <p>In general, two multimaps with identical key-value mappings may or may 361 * not be equal, depending on the implementation. For example, two 362 * {@link SetMultimap} instances with the same key-value mappings are equal, 363 * but equality of two {@link ListMultimap} instances depends on the ordering 364 * of the values for each key. 365 * 366 * <p>A non-empty {@link SetMultimap} cannot be equal to a non-empty 367 * {@link ListMultimap}, since their {@link #asMap} views contain unequal 368 * collections as values. However, any two empty multimaps are equal, because 369 * they both have empty {@link #asMap} views. 370 */ 371 @Override 372 boolean equals(@Nullable Object obj); 373 374 /** 375 * Returns the hash code for this multimap. 376 * 377 * <p>The hash code of a multimap is defined as the hash code of the map view, 378 * as returned by {@link Multimap#asMap}. 379 */ 380 @Override 381 int hashCode(); 382}