Java collections have served the
Java language extremely well. However, they are based upon the idea that all
the elements of a collection exist and are represented somewhere in memory.
This means that they are not capable of representing more-general data, such as
infinite sets.
Consider, for example, the set of all prime numbers.
This cannot be modeled as Set<Integer> because we
don’t know what all the prime numbers are, and we certainly don’t have enough
heap space to represent them all. In Java 7 and earlier versions, the only way
to model the set would be to work with an Iterator representing the set
instead. It is possible to construct a view of data that works primarily with
iterators and relegates the underlying collections to a supporting role. However,
this requires discipline and is not an immediately obvious approach to working
with the Java collections.With Java 7 and earlier versions, if you wanted to use
this type of approach, you would typically depend upon an external library that
provided better support for this functionality. The upcoming release of Java 8
addresses this use case by introducing the Stream interface as an abstraction that
is better suited to dealing with more-general data structures than basic, finite
collections. This means that a Stream can be thought of as more general than an
Iterator or a Collection. However, a Stream is not really a data structure; instead,
it’s an abstraction for handling data. What does this distinction mean? It
means a Stream does not manage the storage for elements nor does it provide a
way to access individual elements directly from the Stream.
No comments:
Post a Comment