java - How to remove and return an object from a Hashset? -
i have hashset has been populated penny objects , want create method remove 1 of these penny objects hashset , return it. here method have created not work error messages. can explain me correct way of doing
public penny removepenny(){ if(penniesset.size()<= 0){ return null; } else penniesset.remove(penny) return penniesset.get(penny) }
penniesset.remove(penny)
, penniesset.get(penny)
not work, penny
class name, not value. if want element set remove, use set.iterator().next()
:
public penny removepenny() { if (penniesset.size() == 0) { return null; } penny penny = penniesset.iterator().next(); penniesset.remove(penny); return penny; }
Comments
Post a Comment