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 -~----------~----~----~----~------~----~------~--~---