Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Removing objects from a hashSet

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Removing objects from a hashSet

    Hey there,

    I'm a bit stuck. I'm desperatly trying to find a way to remove from a HashSet, but everytime I do it, I get the error incompatible types- found java.lang.Objecs but expected Bag, while pointing at the line

    Bag aCan = it.next();

    I don't understand why it expects Bag- as far as I can see, on that line Bag is there. Any help would be great- I'm very much a beginner with this! (Which probably shows)

    import java.util.HashSet;
    import java.util.Iterator;
     
    /**
     * Bag class
     */
    public class Bag
    {
        private HashSet Bag = new HashSet<Can>();
     
        /**
         * Constructor for objects of class Bag
         */
     
        public Bag(int BagAmount)
        {
           HashSet<Can> Bag = new HashSet<Can>();
           for (int i = 0;
                    i < BagAmount;
                    i++){
         Bag.add(new Can());
    }
        }
     
        /**
         * methods
         */
     
        public int CanCount()
     
        {
            return Bag.size();
        }
     
        public Can removeCan()
        {
          if (Bag.size() > 0);
          Iterator it = Bag.iterator();
          while (it.hasNext()) {
          Bag aCan = it.next();
          Bag.remove();
        }
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Removing objects from a hashSet

    First, double check the naming in your code...never name an instance variable identical to its containing class...eg the Bag HashSet identical name as the class which contains it. Second, be careful modifying a Collection while iterating over that collection - depending upon the implementation you could end up getting a ConcurrentModificationException. Lastly, the HashSet generics you define are for can, and unless Bag and Can are somehow related you cannot perform this operation.

Similar Threads

  1. removing stopwords from a word list
    By jessie in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 15th, 2010, 12:02 PM
  2. Removing the middle cell
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 7th, 2010, 01:35 PM
  3. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 12:50 PM
  4. Arrays: question about "removing" items.
    By sanfor in forum Collections and Generics
    Replies: 2
    Last Post: November 30th, 2009, 04:09 AM
  5. [SOLVED] Enhancement in program of removing whitespace from text file
    By John in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: April 27th, 2009, 09:36 AM