Question

Reading this Oracle tutorial I came across this explanation of the difference between the range-view operations of a List and the ones provided by the SortedSet interface.

Here is the bit interested:

The range-view operations are somewhat analogous to those provided by the List interface, but there is one big difference. Range views of a sorted set remain valid even if the backing sorted set is modified directly. This is feasible because the endpoints of a range view of a sorted set are absolute points in the element space rather than specific elements in the backing collection, as is the case for lists.

Is anybody capable to explain the bold part with, let's say, other words?

Thanks in advance.

Was it helpful?

Solution

Let's say you have a list and a set both containing the integers 11, 13, 15 and 17.

You could write set.subSet(12, 15) to construct a view, and then insert 12 into the original set. If you do this, 12 will appear in the view.

This is not possible with the list. Even though you can construct a view, the moment you modify the original list structurally (e.g. insert an element), the view becomes invalid.

OTHER TIPS

The short answer is that sorted sets are backed directly by the set, unlike lists where you are working with, essentially, pointers. Changes to the underlying list changes the pointers (indexes) making holding views of the list for long problemactic. Since a set is sorted and it's a set, you are pointing at specific objects at the range boundry. This means that the references can't become invalid if an insertion or deletion occurs within the range while you hold the view.

More technically, the definition of range in this context:

A range, sometimes known as an interval, is a convex (contiguous) portion of a particular domain. Convexity means that for any a <= b <= c, range.contains(a) && range.contains(c) implies that range.contains(b). Ranges may extend to infinity; for example, the range "x > 3" contains arbitrarily large values -- or may be finitely constrained, for example "2 <= x < 5".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top