I'm working through the SCJP preparation guide and there is something I don't understand about the backed collections.
The book says the following on page 591:
remember that the pollFirstXxx() methods will always remove the first entry from the collection on which they’re invoked, but they will remove an element from the other collection only if it has same value. So it’s most likely that invoking pollFirstXxx() on the copy will remove an entry from both collections, but invoking pollFirstXxx() on the original will remove only the entry from the original.
What I understand is that subMap() or subTree() behaves like a window on the original collection.
So, when an item is removed from the "window", the item will also be removed from the original collection.
The quote above however claims that this is not necessary true when an item is removed from the original.
In trying to understand this I assumed that this feature would be implemented by keeping 2 range variables in the subSet and a reference to the original collection ... but then the behavior described in the qoute can't be explained.
Or do they mean in this quote that invoking pollFirstXxx() on the original will remove only the entry from the original because that first element could not be visible in the "window"/subXxx ???
If the first element of the original is also an element in the subMap/Set then calling pollFirstXxx() on the original WILL remove them from both collections right???
PS: I'm working on some test code to see this confirmed, but still ... don't quite get it theoretically
pollFirstXx() will remove first element from the original Collection. It removes the same from subSet / subMap ONLY if the first element of original is in the sub map. It may not be the case as the sub map/set contains only a range of elements from the original. If the sub map/set does NOT have the first element in original, and if you call pollFirstXxx( ) on original, how can it remove the same from the subSet/Map where it does NOT even exist?
consider the following example. Please go through it, it is self explanatory!
in the above code, the pollFirst() removes element "1" from original; but since sub Set does NOT have "1" it did NOT remove anything from the sub Map.
I was focussing to much on the part "invoking pollFirstXxx() on the original will remove only the entry from the original".
I thought for a minute that I had to interprete that as the changes to the list are unidirectional.
But that's simply because one is a subset of the other.
The sentence in the book is:
So it’s most likely that invoking pollFirstXxx() on the copy will remove an entry from both collections, but invoking pollFirstXxx() on the original will remove only the entry from the original.
The first part of the sentence is not "most likely", but guaranteed.
It should say:
When invoking pollFirstXxx() on the copy it will ALWAYS remove an entry from BOTH collections, but invoking pollFirstXxx() on the original will only remove the element in the copy if it is also present
A minor misinterpretation of mine:-)
Thanks for helping me out!