File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "What will be the output ?" Watch "What will be the output ?" New topic

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();
Iterator iter = ref.iterator();
}//end while loop

}//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
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.


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:
subject: What will be the output ?
It's not a secret anymore!