aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Removing an element from a backed collection. p591 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Removing an element from a backed collection. p591" Watch "Removing an element from a backed collection. p591" New topic
Author

Removing an element from a backed collection. p591

kristof taveirne
Greenhorn

Joined: Sep 21, 2004
Posts: 4
Hi,

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???

Thanks!

Kristof T.

PS: I'm working on some test code to see this confirmed, but still ... don't quite get it theoretically




Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308


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.

take care,


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
kristof taveirne
Greenhorn

Joined: Sep 21, 2004
Posts: 4
Hi,

Thank you very much.

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!

Greetings,
Kristof T.
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

i think that is why K&B added the clause

So it’s most likely that




the key word here is "most likely" ....
Happy Collections!!
 
wood burning stoves
 
subject: Removing an element from a backed collection. p591