Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What will be the output ?

 
Helen Thomas
Ranch Hand
Posts: 1759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sonny Pondrom
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would that be
4 4 3 2 1

 
Helen Thomas
Ranch Hand
Posts: 1759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. Try again concentrating on Collections.
[ July 12, 2004: Message edited by: Helen Thomas ]
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Helen Thomas
Ranch Hand
Posts: 1759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Gian

That is correct and thank you for the fullsome explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic