This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programming Diversions and the fly likes What will be the output ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "What will be the output ?" Watch "What will be the output ?" New topic
Author

What will be the output ?

Helen Thomas
Ranch Hand

Joined: Jan 13, 2004
Posts: 1759
import java.util.TreeSet;
import java.util.Collection;
import java.util.Iterator;

public class AS400{
public static void main(
String args[]){
new Worker().doIt();
}//end main()
}//end class AS400

class Worker{
public void doIt(){
Collection ref = new TreeSet();
Populator.fillIt(ref);
Iterator iter = ref.iterator();
while(iter.hasNext()){
System.out.print(iter.next());
}//end while loop
System.out.println();

}//end doIt()
}// end class Worker

class Populator{
public static void fillIt(
Collection ref){
ref.add(new Integer(4));
ref.add(new Integer(4));
ref.add(new Integer(3));
ref.add(new Integer(2));
ref.add(new Integer(1));
}//end fillIt()
}//end class populator


Le Cafe Mouse - Helen's musings on the web - Java Skills and Thrills
"God who creates and is nature is very difficult to understand, but he is not arbitrary or malicious." OR "God does not play dice." - Einstein
Sonny Pondrom
Ranch Hand

Joined: Jun 05, 2001
Posts: 128
would that be
4 4 3 2 1

Helen Thomas
Ranch Hand

Joined: Jan 13, 2004
Posts: 1759
Not quite. Try again concentrating on Collections.
[ July 12, 2004: Message edited by: Helen Thomas ]
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi Helen,

The answer is 1234.
TreeSet implements SortedSet
and therefore orders it's elements
in ascending order on insertion
in a so-called red-black tree
(if I'm not mistaken ). Anyway
ordering takes place.
Additionally, since it's a Set, each
entry is unique.

Cheers,

Gian Franco
[ July 13, 2004: Message edited by: Gian Franco Casula ]

"Eppur si muove!"
Helen Thomas
Ranch Hand

Joined: Jan 13, 2004
Posts: 1759
Hello Gian

That is correct and thank you for the fullsome explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What will be the output ?