*TreeSet<Object> ts = new TreeSet<Object>();
try with this
*
On Fri, Sep 11, 2009 at 6:48 PM, Deyan Pavlov <[email protected]>wrote:

>
> Hi,
> I understand Treeset is a collection of elements that are comparable.
> But the homework is to put in this collection String, Integer and
> MyOwnClass
> objects, so three very different types.
>
> I think that so far there hasn't been any idea in previous lessons about
> how
> to put all these things together in one group.
>
> So far I have solved all homeworks completely on my own but now this seems
> to be a bit out of boundary exception for me.
>
> Any practical idea how to put these objects together?
>
> Thanks in advance!!
>
>
> ----- Original Message -----
> From: "Ashok A V" <[email protected]>
> To: <[email protected]>
> Cc: "Free Java Programming Online Training Course By Sang Shin"
> <[email protected]>
> Sent: Friday, September 11, 2009 9:45 AM
> Subject: Re: [java programming] Java Collections Framework - lab 1008;
> TreeSet
>
>
> Hi ,
>
> TreeSet is the Set which actually sorts your added items.
> So now the confusion happens when you add objects of different types.
> Say first you add two Strings :
>
> ts.add("one") ;
> ts.add("two") ;
>
> Then you add three Integers :
>
>          ts.add(new Integer(1)) ;
>          ts.add(new Integer(2)) ;
>          ts.add(new Integer(3)) ;
>
> And finally you add MyOwnClass object to the set
>
> MyOwnClass obj1 = new MyOwnClass( );
>  ts.add(obj1) ;
>
> Now remember Tree Set is a collection that sorts the objects in your
> Set collection.
>
> public TreeSet()
>
> Constructs a new, empty set, sorted according to the elements' natural
> order. All elements inserted into the set must implement the
> Comparable interface. Furthermore, all such elements must be mutually
> comparable: e1.compareTo(e2) must not throw a ClassCastException for
> any elements e1 and e2 in the set. If the user attempts to add an
> element to the set that violates this constraint (for example, the
> user attempts to add a string element to a set whose elements are
> integers), the add(Object) call will throw a ClassCastException.
>
> So if you added only string then String Class implements Comparable
> interface which has a compareTo() method. So Sorting of strings is
> possible.The problem occurs when it comes to the integer objects , the
> string class is not able to cast it and it fails saying that :
>
> Exception in thread "main" java.lang.ClassCastException:
> java.lang.String cannot be cast to java.lang.Integer
>
> So if you plan to use TreeSet see to that you add the same kind of
> objects which are sortable by a compareTo() method
>
> For more reading :
> http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeSet.html
>
> Thanks,
> Ashok A V
>
> On Fri, Sep 11, 2009 at 1:17 AM, Deyan Pavlov <[email protected]>
> wrote:
> > Lab 1008, 2.4 homework - TreeSet iterator:
> >
> > import java.util.TreeSet;
> > import java.util.Iterator;
> > import java.util.*;
> >
> > public class Main {
> >
> >
> > public static void main(String[] args) {
> > // TODO code application logic here
> > TreeSet ts = new TreeSet();
> > ts.add("one") ;
> > ts.add("two") ;
> >
> > // HERE comes the problem, trying to add the Integer(1), (2) and (3)
> >
> > ts.add(new Integer(1)) ;
> > ts.add(new Integer(2)) ;
> > ts.add(new Integer(3)) ;
> >
> > MyOwnClass obj1 = new MyOwnClass( );
> > ts.add(obj1) ;
> >
> > }
> > }
> > Result is:
> > Exception in thread "main" java.lang.ClassCastException: java.lang.String
> > cannot be cast to java.lang.Integer
> > at java.lang.Integer.compareTo(Integer.java:35)
> > at java.util.TreeMap.put(TreeMap.java:545)
> > at java.util.TreeSet.add(TreeSet.java:238)
> > at mytreeset.Main.main(Main.java:21)
> > Java Result: 1
> >
> -------------------------------------------------------------------------------------------
> >
> > MyOwnClass obj1 = new MyOwnClass( );
> > ts.add(obj1) ;
> >
> > If I add the two rows above, adding the MyOwnClass obj1, which is
> properly
> > created in another file,
> > then comes an even longer list of errors that didn't happen when applying
> > this same method ADD() in LinkedList, ArrayList and HashSet
> >
> > Exception in thread "main" java.lang.NoClassDefFoundError:
> > mytreeset/MyOwnClass
> > at mytreeset.Main.main(Main.java:16)
> > Caused by: java.lang.ClassNotFoundException: mytreeset.MyOwnClass
> > at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
> > at java.security.AccessController.doPrivileged(Native Method)
> > at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
> > at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
> > at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
> > at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
> > at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
> >
> >
> > Why can't I add objects to the TreeSet, while I could add them with not
> > exceptions in the case of LinkedList, ArrayList and HashSet ?
> >
> > Thank you!!
> > >
> >
>
>
>
> --
> Victory belongs to the most persevering.
>  - Napoleon
>
>
> >
>

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

Reply via email to