Why does Scanner implement Iterator?

前端 未结 4 1895
余生分开走
余生分开走 2021-01-04 08:48

I was just wondering why java.util.Scanner implements java.util.Iterator?

Scanner implements the remove method and throws an UnsupportedOperationExcept

4条回答
  •  旧时难觅i
    2021-01-04 09:41

    For an official answer, see the Collections Framework Overview. Scroll down to the bottom for Design Goals.

    To keep the number of core interfaces small, the interfaces do not attempt to capture such subtle distinctions as mutability, modifiability, and resizability. Instead, certain calls in the core interfaces are optional, enabling implementations to throw an UnsupportedOperationException to indicate that they do not support a specified optional operation. Collection implementers must clearly document which optional operations are supported by an implementation.

    As previous answers have pointed out, the Collections framework could uphold Liskov Substition by decomposing its interfaces into numerous, smaller interfaces. That approach was considered and rejected, in order to minimize the number of core interfaces in the framework.

提交回复
热议问题