This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)
That explains it?
Joined: Sep 10, 2009
Ahh..that might explain. Thanks a lot James.
What's surprising is the output changes with java versions. So with java version "1.4.2_14" the output is
whereas with java version 1.5 and higher, the output is:
I went into src.zip and the addAll method appears to be un-overridden from AbstractCollection, and here it is:So I looked up add in HashSet and found this:Can't see how you manage to double the contents of a Set like that. I altered the original class like thisand got this output
campbell@queeg:~/java$ java TestSet
Type = java.lang.String, hash code = 30, value = 0
Type = java.lang.String, hash code = 31, value = 1
Type = java.lang.String, hash code = 32, value = 2
Type = java.lang.String, hash code = 33, value = 3
Type = java.lang.String, hash code = 34, value = 4
Not adding elements
That is behaving as I would expect, using Java6u14.
Maybe somebody who is getting different outputs would like to try those alterations.