Map.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* Map.java: interface Map -- An object that maps keys to values
  2. interface Map.Entry -- an Entry in a Map
  3. Copyright (C) 1998, 2001, 2004, 2005 Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util;
  33. /**
  34. * An object that maps keys onto values. Keys cannot be duplicated. This
  35. * interface replaces the obsolete {@link Dictionary} abstract class.
  36. * <p>
  37. *
  38. * The map has three collection views, which are backed by the map
  39. * (modifications on one show up on the other): a set of keys, a collection
  40. * of values, and a set of key-value mappings. Some maps have a guaranteed
  41. * order, but not all do.
  42. * <p>
  43. *
  44. * Note: Be careful about using mutable keys. Behavior is unspecified if
  45. * a key's comparison behavior is changed after the fact. As a corollary
  46. * to this rule, don't use a Map as one of its own keys or values, as it makes
  47. * hashCode and equals have undefined behavior.
  48. * <p>
  49. *
  50. * All maps are recommended to provide a no argument constructor, which builds
  51. * an empty map, and one that accepts a Map parameter and copies the mappings
  52. * (usually by putAll), to create an equivalent map. Unfortunately, Java
  53. * cannot enforce these suggestions.
  54. * <p>
  55. *
  56. * The map may be unmodifiable, in which case unsupported operations will
  57. * throw an UnsupportedOperationException. Note that some operations may be
  58. * safe, such as putAll(m) where m is empty, even if the operation would
  59. * normally fail with a non-empty argument.
  60. *
  61. * @author Original author unknown
  62. * @author Eric Blake (ebb9@email.byu.edu)
  63. * @see HashMap
  64. * @see TreeMap
  65. * @see Hashtable
  66. * @see SortedMap
  67. * @see Collection
  68. * @see Set
  69. * @since 1.2
  70. * @status updated to 1.4
  71. */
  72. public interface Map<K, V>
  73. {
  74. /**
  75. * Remove all entries from this Map (optional operation).
  76. *
  77. * @throws UnsupportedOperationException if clear is not supported
  78. */
  79. void clear();
  80. /**
  81. * Returns true if this contains a mapping for the given key.
  82. *
  83. * @param key the key to search for
  84. * @return true if the map contains the key
  85. * @throws ClassCastException if the key is of an inappropriate type
  86. * @throws NullPointerException if key is <code>null</code> but the map
  87. * does not permit null keys
  88. */
  89. boolean containsKey(Object key);
  90. /**
  91. * Returns true if this contains at least one mapping with the given value.
  92. * In other words, returns true if a value v exists where
  93. * <code>(value == null ? v == null : value.equals(v))</code>. This usually
  94. * requires linear time.
  95. *
  96. * @param value the value to search for
  97. * @return true if the map contains the value
  98. * @throws ClassCastException if the type of the value is not a valid type
  99. * for this map.
  100. * @throws NullPointerException if the value is null and the map doesn't
  101. * support null values.
  102. */
  103. boolean containsValue(Object value);
  104. /**
  105. * Returns a set view of the mappings in this Map. Each element in the
  106. * set is a Map.Entry. The set is backed by the map, so that changes in
  107. * one show up in the other. Modifications made while an iterator is
  108. * in progress cause undefined behavior. If the set supports removal,
  109. * these methods remove the underlying mapping from the map:
  110. * <code>Iterator.remove</code>, <code>Set.remove</code>,
  111. * <code>removeAll</code>, <code>retainAll</code>, and <code>clear</code>.
  112. * Element addition, via <code>add</code> or <code>addAll</code>, is
  113. * not supported via this set.
  114. *
  115. * @return the set view of all mapping entries
  116. * @see Map.Entry
  117. */
  118. Set<Map.Entry<K, V>> entrySet();
  119. /**
  120. * Compares the specified object with this map for equality. Returns
  121. * <code>true</code> if the other object is a Map with the same mappings,
  122. * that is,<br>
  123. * <code>o instanceof Map && entrySet().equals(((Map) o).entrySet();</code>
  124. * This allows comparison of maps, regardless of implementation.
  125. *
  126. * @param o the object to be compared
  127. * @return true if the object equals this map
  128. * @see Set#equals(Object)
  129. */
  130. boolean equals(Object o);
  131. /**
  132. * Returns the value mapped by the given key. Returns <code>null</code> if
  133. * there is no mapping. However, in Maps that accept null values, you
  134. * must rely on <code>containsKey</code> to determine if a mapping exists.
  135. *
  136. * @param key the key to look up
  137. * @return the value associated with the key, or null if key not in map
  138. * @throws ClassCastException if the key is an inappropriate type
  139. * @throws NullPointerException if this map does not accept null keys
  140. * @see #containsKey(Object)
  141. */
  142. V get(Object key);
  143. /**
  144. * Associates the given key to the given value (optional operation). If the
  145. * map already contains the key, its value is replaced. Be aware that in
  146. * a map that permits <code>null</code> values, a null return does not
  147. * always imply that the mapping was created.
  148. *
  149. * @param key the key to map
  150. * @param value the value to be mapped
  151. * @return the previous value of the key, or null if there was no mapping
  152. * @throws UnsupportedOperationException if the operation is not supported
  153. * @throws ClassCastException if the key or value is of the wrong type
  154. * @throws IllegalArgumentException if something about this key or value
  155. * prevents it from existing in this map
  156. * @throws NullPointerException if either the key or the value is null,
  157. * and the map forbids null keys or values
  158. * @see #containsKey(Object)
  159. */
  160. V put(K key, V value);
  161. /**
  162. * Returns the hash code for this map. This is the sum of all hashcodes
  163. * for each Map.Entry object in entrySet. This allows comparison of maps,
  164. * regardless of implementation, and satisfies the contract of
  165. * Object.hashCode.
  166. *
  167. * @return the hash code
  168. * @see Map.Entry#hashCode()
  169. */
  170. int hashCode();
  171. /**
  172. * Returns true if the map contains no mappings.
  173. *
  174. * @return true if the map is empty
  175. */
  176. boolean isEmpty();
  177. /**
  178. * Returns a set view of the keys in this Map. The set is backed by the
  179. * map, so that changes in one show up in the other. Modifications made
  180. * while an iterator is in progress cause undefined behavior. If the set
  181. * supports removal, these methods remove the underlying mapping from
  182. * the map: <code>Iterator.remove</code>, <code>Set.remove</code>,
  183. * <code>removeAll</code>, <code>retainAll</code>, and <code>clear</code>.
  184. * Element addition, via <code>add</code> or <code>addAll</code>, is
  185. * not supported via this set.
  186. *
  187. * @return the set view of all keys
  188. */
  189. Set<K> keySet();
  190. /**
  191. * Copies all entries of the given map to this one (optional operation). If
  192. * the map already contains a key, its value is replaced.
  193. *
  194. * @param m the mapping to load into this map
  195. * @throws UnsupportedOperationException if the operation is not supported
  196. * @throws ClassCastException if a key or value is of the wrong type
  197. * @throws IllegalArgumentException if something about a key or value
  198. * prevents it from existing in this map
  199. * @throws NullPointerException if the map forbids null keys or values, or
  200. * if <code>m</code> is null.
  201. * @see #put(Object, Object)
  202. */
  203. void putAll(Map<? extends K, ? extends V> m);
  204. /**
  205. * Removes the mapping for this key if present (optional operation). If
  206. * the key is not present, this returns null. Note that maps which permit
  207. * null values may also return null if the key was removed.
  208. *
  209. * @param key the key to remove
  210. * @return the value the key mapped to, or null if not present.
  211. * @throws UnsupportedOperationException if deletion is unsupported
  212. * @throws NullPointerException if the key is null and this map doesn't
  213. * support null keys.
  214. * @throws ClassCastException if the type of the key is not a valid type
  215. * for this map.
  216. */
  217. V remove(Object o);
  218. /**
  219. * Returns the number of key-value mappings in the map. If there are more
  220. * than Integer.MAX_VALUE mappings, return Integer.MAX_VALUE.
  221. *
  222. * @return the number of mappings
  223. */
  224. int size();
  225. /**
  226. * Returns a collection (or bag) view of the values in this Map. The
  227. * collection is backed by the map, so that changes in one show up in
  228. * the other. Modifications made while an iterator is in progress cause
  229. * undefined behavior. If the collection supports removal, these methods
  230. * remove the underlying mapping from the map: <code>Iterator.remove</code>,
  231. * <code>Collection.remove</code>, <code>removeAll</code>,
  232. * <code>retainAll</code>, and <code>clear</code>. Element addition, via
  233. * <code>add</code> or <code>addAll</code>, is not supported via this
  234. * collection.
  235. *
  236. * @return the collection view of all values
  237. */
  238. Collection<V> values();
  239. /**
  240. * A map entry (key-value pair). The Map.entrySet() method returns a set
  241. * view of these objects; there is no other valid way to come across them.
  242. * These objects are only valid for the duration of an iteration; in other
  243. * words, if you mess with one after modifying the map, you are asking
  244. * for undefined behavior.
  245. *
  246. * @author Original author unknown
  247. * @author Eric Blake (ebb9@email.byu.edu)
  248. * @see Map
  249. * @see Map#entrySet()
  250. * @since 1.2
  251. * @status updated to 1.4
  252. */
  253. interface Entry<K, V>
  254. {
  255. /**
  256. * Get the key corresponding to this entry.
  257. *
  258. * @return the key
  259. */
  260. K getKey();
  261. /**
  262. * Get the value corresponding to this entry. If you already called
  263. * Iterator.remove(), this is undefined.
  264. *
  265. * @return the value
  266. */
  267. V getValue();
  268. /**
  269. * Replaces the value with the specified object (optional operation).
  270. * This writes through to the map, and is undefined if you already
  271. * called Iterator.remove().
  272. *
  273. * @param value the new value to store
  274. * @return the old value
  275. * @throws UnsupportedOperationException if the operation is not supported
  276. * @throws ClassCastException if the value is of the wrong type
  277. * @throws IllegalArgumentException if something about the value
  278. * prevents it from existing in this map
  279. * @throws NullPointerException if the map forbids null values
  280. */
  281. V setValue(V value);
  282. /**
  283. * Returns the hash code of the entry. This is defined as the
  284. * exclusive-or of the hashcodes of the key and value (using 0 for
  285. * <code>null</code>). In other words, this must be:
  286. *
  287. <p><pre>(getKey() == null ? 0 : getKey().hashCode())
  288. ^ (getValue() == null ? 0 : getValue().hashCode())</pre>
  289. *
  290. * @return the hash code
  291. */
  292. int hashCode();
  293. /**
  294. * Compares the specified object with this entry. Returns true only if
  295. * the object is a mapping of identical key and value. In other words,
  296. * this must be:
  297. *
  298. <p><pre>(o instanceof Map.Entry)
  299. && (getKey() == null ? ((Map.Entry) o).getKey() == null
  300. : getKey().equals(((Map.Entry) o).getKey()))
  301. && (getValue() == null ? ((Map.Entry) o).getValue() == null
  302. : getValue().equals(((Map.Entry) o).getValue()))</pre>
  303. *
  304. * @param o the object to compare
  305. *
  306. * @return <code>true</code> if it is equal
  307. */
  308. boolean equals(Object o);
  309. }
  310. }