Results 1 to 2 of 2
  1. #1
    MrKP is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default Trouble with creating TreeSet

    To anyone that can point me in the right direction, I'm attempting to create a TreeSet using the Set interface with doesn't allow duplicates. However, it does allow you to just this:

    Example: ts.add(John, Doe, ISBN:xxxxxxxxxxx, publisher: ToToo); then possibly retrieve the publishers names alphabetically but I received numerous errors using an iterator. My understanding is that the API list set that interfaces with a TreeSet.

    My code is as follows:
    Java Code:
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.TreeSet;
    
    public class SetTest {
    
        private static Object itr;
        private Object aSet;
        private Object bSet;
        private String set;
        private String element;
        private Iterator<String> b_itr;
    
        public static void main(String args[])
        {
        private Object bSet;
        
           private void Iterator ()
           {
               
            //create TreeSet
            TreeSet< String > aSet = new TreeSet<String> aSet );
            
             //Use iterator to display contents of TreeSet
            System.out.println( "Originals contents of Tree are: " );
            Iterator<String> itr = bSet.iterator();
            while(itr.hasNext()){
                String element = itr.next();
                System.out.println( element + " " );
            
            } //end while
            System.out.println();
           
         } //end private
    
           //Modify Objects to iterate
           Iterator<String> b_itr = bSet.Iterator();
           while(b_itr.hasNext()){
               String element = b_itr.next();
               b_itr.set(element + "+");
           }
    
            //Prints the entire list in a sorted order
            System.out.println( "Modified contents of bSet: " );
            itr = bSet.iterator();
            while(itr.hasNext)){
                String str = itr.next();
                System.out.print(str + " ");
            }
            System.out.println();       
    
    	//Objects created Alphabetically in TreeSet listing - can ADD directly
                     TreeSet<Set , String> cSet = new TreeSet<Set , String>();
    
             //TreeSet ingores duplicates and is added right into the data structure
             cSet.add(  "Johnny", "Smith", "0123456789", "HarperCollins" );
             cSet.add("Freddie", "Jones", "0123456777", "HarperCollins");
             cSet.add("Lilly", "Lane", "0122222234", "Penguin");
             cSet.add("Bobby" , "Crosby" , "0111102345" , "Atlantic" );
             cSet.add("Troy", "James", "0119233334", "Atlantic");
    
             //Since its sorted we can ask for various subset
             System.out.println( "Lowest(alphabetically) is" + cSet.first());
             
             //Print bSet greater than "k"
             System.out.println(cSet.tailSet("K").toArray[].length + "cSet higher than \"K\"");
    
             //Prints the entire list alphabetically 
             System.out.println( "Sorted list: " );
             java.util.Iterator - = cSet.iterator();
             while(itr.hasNext())
                 System.out.println(itr.next());
        } //end Main
    I also tried to create a separate instance variable for doing this process
    Last edited by Fubarable; 12-27-2009 at 02:19 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Everything is wrong here, from your trying to have a Set that uses two generic classes (Set and String), to your attempt to use the add method to add multiple Strings to your set object . I suggest you first try a very small program using TreeSet and go from there.

    Also, you need to learn to compile often and not add any new code until the code in hand compiles without errors. Otherwise you'll end up with what you have: a rat's nest of errors, and code that can't be fixed and needs to be tossed.

Similar Threads

  1. Having trouble removing from TreeSet
    By xtrmi in forum New To Java
    Replies: 2
    Last Post: 05-02-2009, 04:01 AM
  2. Replies: 2
    Last Post: 02-23-2009, 02:18 AM
  3. TreeSet Demonstration
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:34 PM
  4. ClassCastException in TreeSet
    By pHew in forum New To Java
    Replies: 2
    Last Post: 01-16-2008, 12:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •