For example, List and Deque both define an encounter order, but their shared supertype, "Collection," does not. Similarly, Set does not establish an encounter order, and neither do its subtypes like HashSet or SortedSet, although LinkedHashSet and SortedSet do exhibit such behavior. Consequently, support for encounter order is scattered throughout the type hierarchy, posing challenges when attempting to express certain useful concepts in APIs. Neither a collection nor a list can adequately describe a parameter or return result that relies on encounter order.
To address this issue, new interfaces for sequenced collections, sequenced sets, and sequenced maps have been introduced, seamlessly integrated into the existing collection type hierarchy. Each of these interfaces includes default implementations for the newly declared methods. Sequenced collections represent collections with a defined encounter order, sequenced sets represent sets that maintain sequence and uniqueness, and sequenced maps represent maps with entries in a specific order. These objects possess unique properties and characteristics.
With the addition of these new interfaces, all sequenced types can process elements in both forward and backward directions using various common iteration mechanisms. These include enhanced for loops, explicit iterator() loops, forEach(), stream(), parallelStream(), and toArray(). This is made possible by the introduction of the reversed() method, which provides a reverse-ordered view of the original collection.
Overall, the Java Collections Framework has made significant advancements by incorporating new interfaces to represent collections with specified encounter order. These interfaces offer a uniform set of actions applicable across such collections. This enhances the framework's comprehensibility and effectiveness for developers, providing standardized and user-friendly support for encounter order.
No comments:
Post a Comment