0

I have a Hashset that has been populated with Penny objects and I want to create a method to remove one of these Penny objects from the HashSet and return it. Here is the method I have created to do this but it does not work as I get error messages. can someone explain to me the correct way of doing this

public Penny removePenny(){

    if(penniesSet.size()<= 0){
        return null;
    }
    else 
        penniesSet.remove(Penny)

    return penniesSet.get(Penny)
}
Baby
  • 5,062
  • 3
  • 30
  • 52
  • What types of error messages? – therealrootuser Dec 02 '14 at 02:40
  • I assume you already put missing `;` in your actual code but you forgot to put in above – Baby Dec 02 '14 at 02:42
  • `penniesSet.remove(Penny)` <--- do you know the difference between an object and a class? Also where is the code that populates the set? And assuming the set has a dozen objects in it, how do you think the code above selects which one of those dozen to remove? – The111 Dec 02 '14 at 02:43
  • This isn't compilable for multiple reasons. I suggest you get started with a book first, Stack Overflow is not a substitute for a book, tutorial or class. – Jeroen Vannevel Dec 02 '14 at 02:45

3 Answers3

1

You are passing a Type Penny to the remove method. You should be passing an object.

As for the get method you should probably read this: Why doesn't java.util.HashSet have a get(Object o) method?

Furthermore, a typical issue that can cause problems looking up an object in a Hashset is if Penny doesn't have a proper hashCode() method.

Removing an arbitrary Penny like this might not be exactly what you were trying to do in the first place. If, you want to remove the last one added (called popping from the stack) you may want to look into using a different collection

Community
  • 1
  • 1
candied_orange
  • 7,036
  • 2
  • 28
  • 62
1

penniesSet.remove(Penny) and penniesSet.get(Penny) will not work, as Penny is class name, not a value. If you want to get any element from the set to remove, use set.iterator().next():

public Penny removePenny() {

    if (penniesSet.size() == 0) {
        return null;
    }
    Penny penny = penniesSet.iterator().next();
    penniesSet.remove(penny);
    return penny;
}
August
  • 12,410
  • 3
  • 35
  • 51
  • This answer is wrong, `remove()` returns a `boolean`. For a correct implementation see my answer. – Óscar López Dec 02 '14 at 02:50
  • @ÓscarLópez Oops, was thinking of `remove` on `Map`. – August Dec 02 '14 at 02:52
  • Did you just copy-paste my own answer, without even giving credit?! Amazing. – Óscar López Dec 02 '14 at 02:52
  • @ÓscarLópez Don't be so keen to attack others... I mistakenly used `remove`, and in fact used the `Iterator` concept before you updated. – August Dec 02 '14 at 02:54
  • "Copy-paste" - absolutely not factual. Look at the casting on your answer. – August Dec 02 '14 at 02:59
  • 1
    I can confirm that August was using the iterator concept when on my screen Oscar's post still had the `penniesSet.remove(Penny)` line (see my comments on his post). I am not sure what "fact" was being pointed out, since all I see is an accusation veiled as a question, but the "fact" that accusation implies is nonexistent. – The111 Dec 02 '14 at 03:05
0

You'll have to make use of Iterator to return and remove the penny object from Hashset. Try following code:

public Penny removePenny(){
    Penny p = null;
    Iterator it = penniesSet.iterator();
    if(it.hasNext()) {
        p = (Penny) it.next();
        it.remove();
    }
    return p;
}
Bokhari
  • 103
  • 1
  • 2
  • 9