wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About iteration order of different data structure 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 "About iteration order of different data structure" Watch "About iteration order of different data structure" New topic
Author

About iteration order of different data structure

Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, I encounter this question in the practice exam book:
import java.util.*;
public class Bucket1 {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
Set<String> lh = new LinkedHashSet<String>();
Set<String> ts = new TreeSet<String>();
List<String> al = new ArrayList<String>();
String[] v = {"3", "2", "A", "2"};
for(int i=0; i< v.length; i++) {
hs.add(v[i]); lh.add(v[i]); ts.add(v[i]); al.add(v[i]);
}
Iterator it = hs.iterator();
while(it.hasNext()) System.out.print(it.next() + " ");
System.out.println();
Iterator it2 = lh.iterator();

while(it2.hasNext()) System.out.print(it2.next() + " ");
System.out.println();
Iterator it3 = ts.iterator();

while(it3.hasNext()) System.out.print(it3.next() + " ");
System.out.println();
Iterator it5 = al.iterator();
while(it5.hasNext()) System.out.print(it5.next() + " ");
System.out.println();
} }



Which statements are true? (Choose all that apply.)
A. An exception is thrown at runtime.
B. Compilation fails due to an error on line 18.
C. "1 3 2" is only guaranteed to be in the output once.
D. "1 2 3" is only guaranteed to be in the output once.
E. "1 3 2" is guaranteed to be in the output more than once.
F. "1 2 3" is guaranteed to be in the output more than once.
G. "1 3 1 2" is guaranteed to be in the output at least once.
H. Compilation fails due to error(s) on lines other than line 18.


Answer (for Objective 6.1):
C, D, and G are correct. The code is all legal and runs without exception. For HashSets,
iteration order is not guaranteed. For LinkedHashSets, iteration order equals insertion
order. For TreeSets, the default iteration order is ascending. For ArrayLists, iteration
order is by index, and when using add(), index “kind of” equals insertion order. Of course,
Sets don’t allow duplicates.

I have a question:
Since hash set does not guarantee any iteration order, the 6 possible outputs are : 132 / 123 / 321 / 312 / 213 / 231
linkedhash set 's output is 132
tree set's output is 123
array list output is 1 3 1 2
There will be 6 possible outputs totally as hash set's iteration order is not guaranteed.
So, choice C says 1 3 2 is only guaranteed to be in the output once. I think this is not true because when hash set's output is 1 32 and linkedhash set's output is 132 as well.
Choice D says 123 is only guaranteed to be in the output once. I think this is not true because hash set may output 123 while tree set output 123.
I think only G is correct.
What do you think?
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
On which page in the books is the question?

John
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
You are interpreting the phrase "only guaranteed to be in the output once" to mean "guaranteed to be in the output exactly once". Probably the authors could have chosen clearer language, but what they mean is "guaranteed to be in the output at least once; additional occurrences are possible but not guaranteed."
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
It would also help in future if you UseCodeTags

Joanne
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, the question is from K&B's practice exam book , exam 4.
John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
In my book it says


I think Dennis is right. "Is only guaranteed to be in the output once." and "Is guaranteed to be in the output at least once." is the same. Meaning it is in the output one ore more time.

John
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Maybe, English is my second language and I don't really understand "is only guaranteed in the output once " means it is guaranteed in the output once only." and "Is guaranteed in the output at least once" means is guaranteed to be in the output at least once.

How can I distinguish between "is only ...once" and " is ....at least once"?
It is just all about my interpretation in English.
For example, we can say " 123 is output only once" and " 123 is output at least once". Is the meaning the same?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About iteration order of different data structure
 
Similar Threads
List in Descending order
Back ground color in Table rows, while using iterator
Can we predict the iteration order of hashset?
getting more dynamic in a result page
Formatting JTextArea