Thank you very much, Dag, solved!

On Mon, Oct 5, 2009 at 4:29 PM, Dag <dvi...@gmail.com> wrote:

>
> Your error message indicates that you should implement Comparable
> instead of Comparator in MyOwnClass. However, doing that will not
> solve the problem. You will then get error messages about comparing
> Integer and String. The problem is that you are adding instances of 3
> different classes to TreeSet and they need to be compared to each
> other. This can be done by implementing a Comparator class that
> compares everything as strings and supply an instance of that class to
> the TreeSet constructor. Here is one way to do it:
>
> ----------------- MyTreeSet.java
>
> package mytreeset;
>
> import java.util.TreeSet;
> import java.util.Iterator;
> import java.util.Comparator;
>
> public class Main {
>
>    public static void main(String[] args) {
>
>         TreeSet ts = new TreeSet(new MyComparator());
>
>        ts.add("Tree Set Item 1");
>        ts.add("Tree Set Item 2");
>
>        MyOwnClass O1 = new MyOwnClass();
>        O1.setShape("Circle");
>        ts.add(O1);
>
>        MyOwnClass O2 = new MyOwnClass();
>        O2.setShape("Triangle");
>        ts.add(O2);
>
>        ts.add(4);
>        ts.add(7);
>        ts.add(9);
>
>        Iterator it = ts.iterator();
>        while(it.hasNext())
>        {
>            System.out.println(it.next());
>        }
>    }
>
>     static class MyComparator implements Comparator
>     {
>        public int compare(Object o1, Object o2)
>        {
>             return o1.toString().compareTo(o2.toString());
>        }
>    }
> }
>
> ----------------- MyOwnClass.java
>
> package mytreeset;
>
> public class MyOwnClass
> {
>    private String Shape;
>
>    public String getShape() {
>        return Shape;
>    }
>
>    public void setShape(String Shape) {
>        this.Shape = Shape;
>    }
>
>    public MyOwnClass() {
>    }
>
>     @Override
>    public String toString()
>    {
>        return this.Shape;
>    }
> }
>
> -----------------
>
> Hope this helps.
> Dag
>
> On Oct 4, 7:58 pm, yongchun gao <gaoyongc...@gmail.com> wrote:
> > Hello, All,
> >
> > I can not figure out how to slove the problem. Could you help me?
> >
> > Here is the classes.
> >
> >
> ------------------------------------------------------------------------------------------------------------
> > package mytreeset;
> >
> > import java.util.TreeSet;
> > import java.util.Iterator;
> >
> > public class Main {
> >
> >     public static void main(String[] args) {
> >
> >         TreeSet ts = new TreeSet();
> >
> >         ts.add("Tree Set Item 1");
> >         ts.add("Tree Set Item 2");
> >
> >         MyOwnClass O1 = new MyOwnClass();
> >         O1.setShape("Circle");
> >         ts.add(O1);
> >
> >         MyOwnClass O2 = new MyOwnClass();
> >         O2.setShape("Triangle");
> >         ts.add(O2);
> >
> >         ts.add(4);
> >         ts.add(7);
> >         ts.add(9);
> >
> >         Iterator it = ts.iterator();
> >         while(it.hasNext())
> >         {
> >             System.out.println(it.next());
> >         }
> >     }
> >
> > }
> >
> >
> ------------------------------------------------------------------------------------------------------------
> >
> > package mytreeset;
> >
> > import java.util.Comparator;
> >
> > public class MyOwnClass implements Comparator
> > {
> >     private String Shape;
> >
> >     public String getShape() {
> >         return Shape;
> >     }
> >
> >     public void setShape(String Shape) {
> >         this.Shape = Shape;
> >     }
> >
> >     public MyOwnClass() {
> >     }
> >
> >     public boolean equals(Object obj)
> >     {
> >         MyOwnClass M1=(MyOwnClass) obj;
> >         if (M1.Shape.length()==this.Shape.length())
> >             return true;
> >         else
> >             return false;
> >     }
> >
> >     public int compare(Object o1, Object o2) {
> >         MyOwnClass M1=(MyOwnClass) o1;
> >         MyOwnClass M2=(MyOwnClass) o2;
> >         if (M1.Shape.length()>M2.Shape.length())
> >             return 1;
> >         else if (M1.Shape.length()==M2.Shape.length())
> >             return 0;
> >         else
> >             return -1;
> >     }
> >
> > }
> >
> >
> ------------------------------------------------------------------------------------------------------------
> > When I run the main class,  I got
> >
> > run:
> > Exception in thread "main" java.lang.ClassCastException:
> > mytreeset.MyOwnClass cannot be cast to java.lang.Comparable
> >         at java.util.TreeMap.put(TreeMap.java:542)
> >         at java.util.TreeSet.add(TreeSet.java:238)
> >         at mytreeset.Main.main(Main.java:18)
> > Java Result: 1
> > BUILD SUCCESSFUL (total time: 0 seconds)
> >
> > Thanks
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to