jdk/src/java.base/share/classes/java/util/doc-files/coll-designfaq.html
changeset 46900 e92e67ed12b4
parent 45647 ccaa2a452a2d
equal deleted inserted replaced
46899:ea81af6257b3 46900:e92e67ed12b4
     1 <?xml version="1.0" encoding="utf-8"?>
     1 <!DOCTYPE html>
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       
     3     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
     4 
       
     5 <!--
     2 <!--
     6  Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
     3  Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
     7  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     8 
     5 
     9  This code is free software; you can redistribute it and/or modify it
     6  This code is free software; you can redistribute it and/or modify it
    24 
    21 
    25  Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    26  or visit www.oracle.com if you need additional information or have any
    23  or visit www.oracle.com if you need additional information or have any
    27  questions.
    24  questions.
    28 -->
    25 -->
    29 
    26 <html lang="en-US">
    30 <html lang="en-US" xmlns="http://www.w3.org/1999/xhtml" xml:lang=
       
    31 "en-US">
       
    32 <head>
    27 <head>
    33 <title>Java Collections API Design FAQ</title>
    28 <title>Java Collections API Design FAQ</title>
       
    29 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    34 </head>
    30 </head>
    35 <body>
    31 <body>
    36 <h2>Java Collections API Design FAQ</h2>
    32 <h2>Java Collections API Design FAQ</h2>
    37 <!-- Body text begins here -->
    33 <!-- Body text begins here -->
    38 <hr />
    34 <hr>
    39 This document answers frequently asked questions concerning the
    35 This document answers frequently asked questions concerning the
    40 design of the Java collections framework. It is derived from the
    36 design of the Java collections framework. It is derived from the
    41 large volume of traffic on the collections-comments alias. It
    37 large volume of traffic on the collections-comments alias. It
    42 serves as a design rationale for the collections framework.
    38 serves as a design rationale for the collections framework.
    43 <h3>Core Interfaces - General Questions</h3>
    39 <h3>Core Interfaces - General Questions</h3>
   103 aliasing.</b></a></li>
    99 aliasing.</b></a></li>
   104 <li><a href="#a27"><b>Why don't you provide for "observable"
   100 <li><a href="#a27"><b>Why don't you provide for "observable"
   105 collections that send out Events when they're
   101 collections that send out Events when they're
   106 modified?</b></a></li>
   102 modified?</b></a></li>
   107 </ol>
   103 </ol>
   108 <hr size="3" noshade="noshade" />
   104 <hr>
   109 <h3>Core Interfaces - General Questions</h3>
   105 <h3>Core Interfaces - General Questions</h3>
   110 <ol>
   106 <ol>
   111 <li><a name="a1" id="a1"><b>Why don't you support immutability
   107 <li><a id="a1"><b>Why don't you support immutability
   112 directly in the core collection interfaces so that you can do away
   108 directly in the core collection interfaces so that you can do away
   113 with <em>optional operations</em> (and
   109 with <em>optional operations</em> (and
   114 UnsupportedOperationException)?</b></a>
   110 UnsupportedOperationException)?</b></a>
   115 <p>This is the most controversial design decision in the whole API.
   111 <p>This is the most controversial design decision in the whole API.
   116 Clearly, static (compile time) type checking is highly desirable,
   112 Clearly, static (compile time) type checking is highly desirable,
   166 <p>When all was said and done, we felt that it was a sound
   162 <p>When all was said and done, we felt that it was a sound
   167 engineering compromise to sidestep the whole issue by providing a
   163 engineering compromise to sidestep the whole issue by providing a
   168 very small set of core interfaces that can throw a runtime
   164 very small set of core interfaces that can throw a runtime
   169 exception.</p>
   165 exception.</p>
   170 </li>
   166 </li>
   171 <li><a name="a2" id="a2"><b>Won't programmers have to surround any
   167 <li><a id="a2"><b>Won't programmers have to surround any
   172 code that calls optional operations with a try-catch clause in case
   168 code that calls optional operations with a try-catch clause in case
   173 they throw an UnsupportedOperationException?</b></a>
   169 they throw an UnsupportedOperationException?</b></a>
   174 <p>It was never our intention that programs should catch these
   170 <p>It was never our intention that programs should catch these
   175 exceptions: that's why they're unchecked (runtime) exceptions. They
   171 exceptions: that's why they're unchecked (runtime) exceptions. They
   176 should only arise as a result of programming errors, in which case,
   172 should only arise as a result of programming errors, in which case,
   177 your program will halt due to the uncaught exception.</p>
   173 your program will halt due to the uncaught exception.</p>
   178 </li>
   174 </li>
   179 <li><a name="a3" id="a3"><b>Why isn't there a core interface for
   175 <li><a id="a3"><b>Why isn't there a core interface for
   180 "bags" (AKA multisets)?</b></a>
   176 "bags" (AKA multisets)?</b></a>
   181 <p>The Collection interface provides this functionality. We are not
   177 <p>The Collection interface provides this functionality. We are not
   182 providing any public implementations of this interface, as we think
   178 providing any public implementations of this interface, as we think
   183 that it wouldn't be used frequently enough to "pull its weight." We
   179 that it wouldn't be used frequently enough to "pull its weight." We
   184 occasionally return such Collections, which are implemented easily
   180 occasionally return such Collections, which are implemented easily
   185 atop AbstractCollection (for example, the Collection returned by
   181 atop AbstractCollection (for example, the Collection returned by
   186 Map.values).</p>
   182 Map.values).</p>
   187 </li>
   183 </li>
   188 <li><a name="a28" id="a28"><b>Why didn't you use "Beans-style
   184 <li><a id="a28"><b>Why didn't you use "Beans-style
   189 names" for consistency?</b></a>
   185 names" for consistency?</b></a>
   190 <p>While the names of the new collections methods do not adhere to
   186 <p>While the names of the new collections methods do not adhere to
   191 the "Beans naming conventions", we believe that they are
   187 the "Beans naming conventions", we believe that they are
   192 reasonable, consistent and appropriate to their purpose. It should
   188 reasonable, consistent and appropriate to their purpose. It should
   193 be remembered that the Beans naming conventions do not apply to the
   189 be remembered that the Beans naming conventions do not apply to the
   205 a long expression. If we named the methods "getIterator",
   201 a long expression. If we named the methods "getIterator",
   206 "hasNextElement" and "getNextElement", this would no longer be the
   202 "hasNextElement" and "getNextElement", this would no longer be the
   207 case. Thus, we adopted the "traditional" JDK style rather than the
   203 case. Thus, we adopted the "traditional" JDK style rather than the
   208 Beans style.</li>
   204 Beans style.</li>
   209 </ol>
   205 </ol>
   210 <hr />
   206 <hr>
   211 <h3>Collection Interface</h3>
   207 <h3>Collection Interface</h3>
   212 <ol>
   208 <ol>
   213 <li><a name="a5" id="a5"><b>Why doesn't Collection extend Cloneable
   209 <li><a id="a5"><b>Why doesn't Collection extend Cloneable
   214 and Serializable?</b></a>
   210 and Serializable?</b></a>
   215 <p>Many Collection implementations (including all of the ones
   211 <p>Many Collection implementations (including all of the ones
   216 provided by the JDK) will have a public clone method, but it would
   212 provided by the JDK) will have a public clone method, but it would
   217 be mistake to require it of all Collections. For example, what does
   213 be mistake to require it of all Collections. For example, what does
   218 it mean to clone a Collection that's backed by a terabyte SQL
   214 it mean to clone a Collection that's backed by a terabyte SQL
   222 much more flexible and less error prone to have the client decide
   218 much more flexible and less error prone to have the client decide
   223 what type of Collection is desired, create an empty Collection of
   219 what type of Collection is desired, create an empty Collection of
   224 this type, and use the addAll method to copy the elements of the
   220 this type, and use the addAll method to copy the elements of the
   225 original collection into the new one.</p>
   221 original collection into the new one.</p>
   226 </li>
   222 </li>
   227 <li><a name="a6" id="a6"><b>Why don't you provide an "apply" method
   223 <li><a id="a6"><b>Why don't you provide an "apply" method
   228 in Collection to apply a given method ("upcall") to all the
   224 in Collection to apply a given method ("upcall") to all the
   229 elements of the Collection?</b></a>
   225 elements of the Collection?</b></a>
   230 <p>This is what is referred to as an "Internal Iterator" in the
   226 <p>This is what is referred to as an "Internal Iterator" in the
   231 "Design Patterns" book (Gamma et al.). We considered providing it,
   227 "Design Patterns" book (Gamma et al.). We considered providing it,
   232 but decided not to as it seems somewhat redundant to support
   228 but decided not to as it seems somewhat redundant to support
   233 internal and external iterators, and Java already has a precedent
   229 internal and external iterators, and Java already has a precedent
   234 for external iterators (with Enumerations). The "throw weight" of
   230 for external iterators (with Enumerations). The "throw weight" of
   235 this functionality is increased by the fact that it requires a
   231 this functionality is increased by the fact that it requires a
   236 public interface to describe upcalls.</p>
   232 public interface to describe upcalls.</p>
   237 </li>
   233 </li>
   238 <li><a name="a7" id="a7"><b>Why didn't you provide a "Predicate"
   234 <li><a id="a7"><b>Why didn't you provide a "Predicate"
   239 interface, and related methods (e.g., a method to find the first
   235 interface, and related methods (e.g., a method to find the first
   240 element in the Collection satisfying the predicate)?</b></a>
   236 element in the Collection satisfying the predicate)?</b></a>
   241 <p>It's easy to implement this functionality atop Iterators, and
   237 <p>It's easy to implement this functionality atop Iterators, and
   242 the resulting code may actually look cleaner as the user can inline
   238 the resulting code may actually look cleaner as the user can inline
   243 the predicate. Thus, it's not clear whether this facility pulls its
   239 the predicate. Thus, it's not clear whether this facility pulls its
   244 weight. It could be added to the Collections class at a later date
   240 weight. It could be added to the Collections class at a later date
   245 (implemented atop Iterator), if it's deemed useful.</p>
   241 (implemented atop Iterator), if it's deemed useful.</p>
   246 </li>
   242 </li>
   247 <li><a name="a8" id="a8"><b>Why don't you provide a form of the
   243 <li><a id="a8"><b>Why don't you provide a form of the
   248 addAll method that takes an Enumeration (or an Iterator)?</b></a>
   244 addAll method that takes an Enumeration (or an Iterator)?</b></a>
   249 <p>Because we don't believe in using Enumerations (or Iterators) as
   245 <p>Because we don't believe in using Enumerations (or Iterators) as
   250 "poor man's collections." This was occasionally done in prior
   246 "poor man's collections." This was occasionally done in prior
   251 releases, but now that we have the Collection interface, it is the
   247 releases, but now that we have the Collection interface, it is the
   252 preferred way to pass around abstract collections of objects.</p>
   248 preferred way to pass around abstract collections of objects.</p>
   253 </li>
   249 </li>
   254 <li><a name="a9" id="a9"><b>Why don't the concrete implementations
   250 <li><a id="a9"><b>Why don't the concrete implementations
   255 in the JDK have Enumeration (or Iterator) constructors?</b></a>
   251 in the JDK have Enumeration (or Iterator) constructors?</b></a>
   256 <p>Again, this is an instance of an Enumeration serving as a "poor
   252 <p>Again, this is an instance of an Enumeration serving as a "poor
   257 man's collection" and we're trying to discourage that. Note
   253 man's collection" and we're trying to discourage that. Note
   258 however, that we strongly suggest that all concrete implementations
   254 however, that we strongly suggest that all concrete implementations
   259 should have constructors that take a Collection (and create a new
   255 should have constructors that take a Collection (and create a new
   260 Collection with the same elements).</p>
   256 Collection with the same elements).</p>
   261 </li>
   257 </li>
   262 <li><a name="a10" id="a10"><b>Why don't you provide an Iterator.add
   258 <li><a id="a10"><b>Why don't you provide an Iterator.add
   263 method?</b></a>
   259 method?</b></a>
   264 <p>The semantics are unclear, given that the contract for Iterator
   260 <p>The semantics are unclear, given that the contract for Iterator
   265 makes no guarantees about the order of iteration. Note, however,
   261 makes no guarantees about the order of iteration. Note, however,
   266 that ListIterator does provide an add operation, as it does
   262 that ListIterator does provide an add operation, as it does
   267 guarantee the order of the iteration.</p>
   263 guarantee the order of the iteration.</p>
   268 </li>
   264 </li>
   269 </ol>
   265 </ol>
   270 <hr />
   266 <hr>
   271 <h3>List Interface</h3>
   267 <h3>List Interface</h3>
   272 <ol>
   268 <ol>
   273 <li><a name="a11" id="a11"><b>Why don't you rename the List
   269 <li><a id="a11"><b>Why don't you rename the List
   274 interface to Sequence; doesn't "list" generally suggest "linked
   270 interface to Sequence; doesn't "list" generally suggest "linked
   275 list"? Also, doesn't it conflict with java.awt.List?</b></a>
   271 list"? Also, doesn't it conflict with java.awt.List?</b></a>
   276 <p>People were evenly divided as to whether List suggests linked
   272 <p>People were evenly divided as to whether List suggests linked
   277 lists. Given the implementation naming convention,
   273 lists. Given the implementation naming convention,
   278 &lt;<em>Implementation</em>&gt;&lt;<em>Interface</em>&gt;, there
   274 &lt;<em>Implementation</em>&gt;&lt;<em>Interface</em>&gt;, there
   283 <pre>
   279 <pre>
   284     import java.util.*;
   280     import java.util.*;
   285     import java.awt.*;
   281     import java.awt.*;
   286     import java.util.List;   // Dictates interpretation of "List"
   282     import java.util.List;   // Dictates interpretation of "List"
   287 </pre></li>
   283 </pre></li>
   288 <li><a name="a12" id="a12"><b>Why don't you rename List's set
   284 <li><a id="a12"><b>Why don't you rename List's set
   289 method to replace, to avoid confusion with Set.</b></a>
   285 method to replace, to avoid confusion with Set.</b></a>
   290 <p>It was decided that the "set/get" naming convention was strongly
   286 <p>It was decided that the "set/get" naming convention was strongly
   291 enough enshrined in the language that we'd stick with it.</p>
   287 enough enshrined in the language that we'd stick with it.</p>
   292 </li>
   288 </li>
   293 </ol>
   289 </ol>
   294 <hr />
   290 <hr>
   295 <h3>Map Interface</h3>
   291 <h3>Map Interface</h3>
   296 <ol>
   292 <ol>
   297 <li><a name="a14" id="a14"><b>Why doesn't Map extend
   293 <li><a id="a14"><b>Why doesn't Map extend
   298 Collection?</b></a>
   294 Collection?</b></a>
   299 <p>This was by design. We feel that mappings are not collections
   295 <p>This was by design. We feel that mappings are not collections
   300 and collections are not mappings. Thus, it makes little sense for
   296 and collections are not mappings. Thus, it makes little sense for
   301 Map to extend the Collection interface (or vice versa).</p>
   297 Map to extend the Collection interface (or vice versa).</p>
   302 <p>If a Map is a Collection, what are the elements? The only
   298 <p>If a Map is a Collection, what are the elements? The only
   315 the List changes the Key associated with every element before the
   311 the List changes the Key associated with every element before the
   316 deleted element. That's why we don't have a map view operation on
   312 deleted element. That's why we don't have a map view operation on
   317 Lists.</p>
   313 Lists.</p>
   318 </li>
   314 </li>
   319 </ol>
   315 </ol>
   320 <hr />
   316 <hr>
   321 <h3>Iterator Interface</h3>
   317 <h3>Iterator Interface</h3>
   322 <ol>
   318 <ol>
   323 <li><a name="a18" id="a18"><b>Why doesn't Iterator extend
   319 <li><a id="a18"><b>Why doesn't Iterator extend
   324 Enumeration?</b></a>
   320 Enumeration?</b></a>
   325 <p>We view the method names for Enumeration as unfortunate. They're
   321 <p>We view the method names for Enumeration as unfortunate. They're
   326 very long, and very frequently used. Given that we were adding a
   322 very long, and very frequently used. Given that we were adding a
   327 method and creating a whole new framework, we felt that it would be
   323 method and creating a whole new framework, we felt that it would be
   328 foolish not to take advantage of the opportunity to improve the
   324 foolish not to take advantage of the opportunity to improve the
   329 names. Of course we could support the new and old names in
   325 names. Of course we could support the new and old names in
   330 Iterator, but it doesn't seem worthwhile.</p>
   326 Iterator, but it doesn't seem worthwhile.</p>
   331 </li>
   327 </li>
   332 <li><a name="a19" id="a19"><b>Why don't you provide an
   328 <li><a id="a19"><b>Why don't you provide an
   333 Iterator.peek method that allows you to look at the next element in
   329 Iterator.peek method that allows you to look at the next element in
   334 an iteration without advancing the iterator?</b></a>
   330 an iteration without advancing the iterator?</b></a>
   335 <p>It can be implemented atop the current Iterators (a similar
   331 <p>It can be implemented atop the current Iterators (a similar
   336 pattern to java.io.PushbackInputStream). We believe that its use
   332 pattern to java.io.PushbackInputStream). We believe that its use
   337 would be rare enough that it isn't worth including in the interface
   333 would be rare enough that it isn't worth including in the interface
   338 that everyone has to implement.</p>
   334 that everyone has to implement.</p>
   339 </li>
   335 </li>
   340 </ol>
   336 </ol>
   341 <hr />
   337 <hr>
   342 <h3>Miscellaneous</h3>
   338 <h3>Miscellaneous</h3>
   343 <ol>
   339 <ol>
   344 <li><a name="a23" id="a23"><b>Why did you write a new collections
   340 <li><a id="a23"><b>Why did you write a new collections
   345 framework instead of adopting JGL (a preexisting collections
   341 framework instead of adopting JGL (a preexisting collections
   346 package from ObjectSpace, Inc.) into the JDK?</b></a>
   342 package from ObjectSpace, Inc.) into the JDK?</b></a>
   347 <p>If you examine the goals for our Collections framework (in the
   343 <p>If you examine the goals for our Collections framework (in the
   348 Overview), you'll see that we are not really "playing in the same
   344 Overview), you'll see that we are not really "playing in the same
   349 space" as JGL. Quoting from the "Design Goals" Section of the Java
   345 space" as JGL. Quoting from the "Design Goals" Section of the Java
   361 we feel that it will be good for Java in the long run. As the Java
   357 we feel that it will be good for Java in the long run. As the Java
   362 libraries mature, they inevitably grow, but we are trying as hard
   358 libraries mature, they inevitably grow, but we are trying as hard
   363 as we can to keep them small and manageable, so that Java continues
   359 as we can to keep them small and manageable, so that Java continues
   364 to be an easy, fun language to learn and to use.</p>
   360 to be an easy, fun language to learn and to use.</p>
   365 </li>
   361 </li>
   366 <li><a name="a26" id="a26"><b>Why don't you eliminate all of the
   362 <li><a id="a26"><b>Why don't you eliminate all of the
   367 methods and classes that return "views" (Collections backed by
   363 methods and classes that return "views" (Collections backed by
   368 other collection-like objects). This would greatly reduce
   364 other collection-like objects). This would greatly reduce
   369 aliasing.</b></a>
   365 aliasing.</b></a>
   370 <p>Given that we provide core collection interfaces behind which
   366 <p>Given that we provide core collection interfaces behind which
   371 programmers can "hide" their own implementations, there will be
   367 programmers can "hide" their own implementations, there will be
   378 "../List.html#subList-int-int-">List.subList</a>.
   374 "../List.html#subList-int-int-">List.subList</a>.
   379 The existence of this method means that people who write methods
   375 The existence of this method means that people who write methods
   380 taking List on input do not have to write secondary forms taking an
   376 taking List on input do not have to write secondary forms taking an
   381 offset and a length (as they do for arrays).</p>
   377 offset and a length (as they do for arrays).</p>
   382 </li>
   378 </li>
   383 <li><a name="a27" id="a27"><b>Why don't you provide for
   379 <li><a id="a27"><b>Why don't you provide for
   384 "observable" collections that send out Events when they're
   380 "observable" collections that send out Events when they're
   385 modified?</b></a>
   381 modified?</b></a>
   386 <p>Primarily, resource constraints. If we're going to commit to
   382 <p>Primarily, resource constraints. If we're going to commit to
   387 such an API, it has to be something that works for everyone, that
   383 such an API, it has to be something that works for everyone, that
   388 we can live with for the long haul. We may provide such a facility
   384 we can live with for the long haul. We may provide such a facility
   389 some day. In the meantime, it's not difficult to implement such a
   385 some day. In the meantime, it's not difficult to implement such a
   390 facility on top of the public APIs.</p>
   386 facility on top of the public APIs.</p>
   391 </li>
   387 </li>
   392 </ol>
   388 </ol>
   393 <hr />
   389 <hr>
   394 <p style="font-size:smaller">
   390 <p style="font-size:smaller">
   395 Copyright &copy; 1998, 2017, Oracle and/or its affiliates. 500 Oracle Parkway<br />
   391 Copyright &copy; 1998, 2017, Oracle and/or its affiliates. 500 Oracle Parkway<br>
   396     Redwood Shores, CA 94065 USA. All rights reserved.</p>
   392     Redwood Shores, CA 94065 USA. All rights reserved.</p>
   397 <!-- Body text ends here -->
   393 <!-- Body text ends here -->
   398 </body>
   394 </body>
   399 </html>
   395 </html>