1 2 Previous Next 15 Replies Latest reply: Jan 22, 2008 11:46 PM by 807601 RSS

    How to iterate elements in TreeSet in java ?

    796213
      import java.util.*;
      public class Drink implements Comparable {
      public String name;
      
      public int compareTo(Object o) {
      return 0;
      }
      public static void main(String[] args)
      {
      Drink one = new Drink();
      Drink two = new Drink();
      one.name = "Coffee".
      two.name = "Tea".
      TreeSet set = new TreeSet();
      set.add(one);
      set.add(two);
      
      } 
      }
        • 1. Re: How to iterate elements in TreeSet in java ?
          800323
          Use the interator() method:
          http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeSet.html#iterator()
          • 2. Re: How to iterate elements in TreeSet in java ?
            795628
            Iterator it = set.iterator();
            Drink someDrink;
            while (it.hasNext()) {
            someDrink = (Drink)it.next();
            }
            • 3. Re: How to iterate elements in TreeSet in java ?
              796213
              Iterator it = set.iterator();
              Drink someDrink;
              while (it.hasNext()) {
              someDrink = (Drink)it.next();
              }
              import java.util.*;
              public class Drink implements Comparable {
              public String name;
              
              public int compareTo(Object o) {
              return 0;
              }
              public static void main(String[] args)
              {
              Drink one = new Drink();
              Drink two = new Drink();
              one.name = "Coffee";
              two.name = "Tea";
              TreeSet set = new TreeSet();
              set.add(one);
              set.add(two);
              Iterator it = set.iterator();
              while(it.hasNext())
              {
              Drink someone = (Drink)it.next();
              System.out.print(someone);
              }
              } 
              }
              E:\>javac Drink.java
              Note: Drink.java uses unchecked or unsafe operations.
              Note: Recompile with -Xlint:unchecked for details.

              E:\>java Drink
              Drink@10b62c9
              • 4. Re: How to iterate elements in TreeSet in java ?
                800323
                Iterator it = set.iterator();
                Drink someDrink;
                while (it.hasNext()) {
                someDrink = (Drink)it.next();
                }
                import java.util.*;
                public class Drink implements Comparable {
                public String name;
                
                public int compareTo(Object o) {
                return 0;
                }
                public static void main(String[] args)
                {
                Drink one = new Drink();
                Drink two = new Drink();
                one.name = "Coffee";
                two.name = "Tea";
                TreeSet set = new TreeSet();
                set.add(one);
                set.add(two);
                Iterator it = set.iterator();
                while(it.hasNext())
                {
                Drink someone = (Drink)it.next();
                System.out.print(someone);
                }
                } 
                }
                E:\>javac Drink.java
                Note: Drink.java uses unchecked or unsafe
                operations.
                Note: Recompile with -Xlint:unchecked for details.

                E:\>java Drink
                Drink@10b62c9
                Are you just showing us your completed code or do you have another question?
                • 5. Re: How to iterate elements in TreeSet in java ?
                  796213
                  Iterator it = set.iterator();
                  Drink someDrink;
                  while (it.hasNext()) {
                  someDrink = (Drink)it.next();
                  }
                  import java.util.*;
                  public class Drink implements Comparable {
                  public String name;
                  
                  public int compareTo(Object o) {
                  return 0;
                  }
                  public static void main(String[] args)
                  {
                  Drink one = new Drink();
                  Drink two = new Drink();
                  one.name = "Coffee";
                  two.name = "Tea";
                  TreeSet set = new TreeSet();
                  set.add(one);
                  set.add(two);
                  Iterator it = set.iterator();
                  while(it.hasNext())
                  {
                  Drink someone = (Drink)it.next();
                  System.out.print(someone);
                  }
                  } 
                  }
                  E:\>javac Drink.java
                  Note: Drink.java uses unchecked or unsafe
                  operations.
                  Note: Recompile with -Xlint:unchecked for details.

                  E:\>java Drink
                  Drink@10b62c9
                  Are you just showing us your completed code or do you
                  have another question?
                  why this is showing a hash value when i need a name of the drink ?
                  • 6. Re: How to iterate elements in TreeSet in java ?
                    796447
                    Drink someone = (Drink)it.next();
                    System.out.print(someone);
                    why this is showing a hash value when i need a name
                    of the drink ?
                    Where do you think you told it to print the Drink object's "name" field? You told it to print the Drink object itself, not its name field.
                    • 7. Re: How to iterate elements in TreeSet in java ?
                      796213
                      Drink someone = (Drink)it.next();
                      System.out.print(someone);
                      why this is showing a hash value when i need a
                      name
                      of the drink ?
                      Where do you think you told it to print the Drink
                      object's "name" field? You told it to print the Drink
                      object itself, not its name field.
                      another thing compareTo is returning 0.
                      • 8. Re: How to iterate elements in TreeSet in java ?
                        796447
                        another thing compareTo is returning 0.
                        Not surprising, considering how you implemented that method:
                        public int compareTo(Object o) {
                        return 0;
                        }
                        • 9. Re: How to iterate elements in TreeSet in java ?
                          800323
                          why this is showing a hash value when i need a name
                          of the drink ?
                          System.out.print(someone);

                          The above statement just calls the toString() method on the object. If you do not override that method then it just performs Object's toString() method.
                          • 10. Re: How to iterate elements in TreeSet in java ?
                            JosAH
                            As long as your compareTo method thinks all Drinks are equal,
                            your TreeSet will only be able to store one Drink, all others will be
                            considered equal. Fix that compareTo method first.

                            kind regards,

                            Jos
                            • 11. Re: How to iterate elements in TreeSet in java ?
                              795628
                              import java.util.*;
                              
                              public class Drink implements Comparable {
                              
                                   public String name;
                              
                                   public int compareTo(Object obj) {
                                        Drink otherDrink = (Drink)obj;
                                        return this.name.compareTo(otherDrink.name);
                                   }
                              
                                   public String toString() {
                                        return name;
                                   }
                                   
                                   public static void main(String[] args) {
                                        Drink one = new Drink();
                                        Drink two = new Drink();
                                        one.name = "Coffee";
                                        two.name = "Tea";
                                        TreeSet set = new TreeSet();
                                        set.add(one);
                                        set.add(two);
                                        Iterator it = set.iterator();
                                        Drink someone;
                                        while(it.hasNext()) {
                                             someone = (Drink)it.next();
                                             System.out.println(someone);
                                        }
                                   } 
                              
                              }
                              • 12. Re: How to iterate elements in TreeSet in java ?
                                800308
                                Gents,

                                You would be getting annoying compiler warnings like this...
                                ---------- compile ----------
                                Drink.java:21: warning: [unchecked] unchecked conversion
                                found   : java.util.TreeSet
                                required: java.util.TreeSet<Drink>
                                          TreeSet<Drink> drinks = new TreeSet();
                                                                        ^
                                1 warning
                                
                                Output completed (0 sec consumed) - Normal Termination
                                Well .... here's a clean, succinct version with meaninful variable names and everything... Well golly gee...
                                import java.util.*;
                                
                                public class Drink implements Comparable {
                                
                                     public String name;
                                
                                     Drink(String name) {
                                          this.name = name;
                                     }
                                
                                     public int compareTo(Object otherObject) {
                                          Drink otherDrink = (Drink)otherObject;
                                          return this.name.compareTo(otherDrink.name);
                                     }
                                
                                     public static void main(String[] args) {
                                          TreeSet<Drink> drinks = new TreeSet<Drink>();
                                          drinks.add(new Drink("Coffee"));
                                          drinks.add(new Drink("Tea"));
                                          Iterator<Drink> thisDrink = drinks.iterator();
                                          while(thisDrink.hasNext()) {
                                               System.out.println(thisDrink.next().name);
                                          }
                                     }
                                }
                                Suggested 2 minutes: http://www.rgagnon.com/javadetails/java-0521.html for all you chickens.

                                Suggested 2 hours: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf#search=%22sun%20java%20generics%22 for a thorough (say heavy) explanation of generics... only 70kb.

                                Ain't google grand.

                                Keith.
                                • 13. Re: How to iterate elements in TreeSet in java ?
                                  807601
                                  can u tell me the output of the said program and explain me hw it is got
                                  • 14. Re: How to iterate elements in TreeSet in java ?
                                    807601
                                    corlettk wrote:
                                         public static void main(String[] args) {
                                              TreeSet<Drink> drinks = new TreeSet<Drink>();
                                              drinks.add(new Drink("Coffee"));
                                              drinks.add(new Drink("Tea"));
                                              Iterator<Drink> thisDrink = drinks.iterator();
                                              while(thisDrink.hasNext()) {
                                                   System.out.println(thisDrink.next().name);
                                              }
                                         }
                                    }
                                    Of course this can also use the enchanced for loop so lines from Iterator declaration can be changed to:
                                    for (Drink d : drinks) {
                                       System.out.println(d.name);
                                    }
                                    1 2 Previous Next