php implementation of Java HashSet

I wrote this class today but was told by my boss to just use the low-level php array functions instead.

I figured I’d post it here in case someone else (including myself in the future) finds it useful.

/**
 * PHP implementation of Java HashSet
 *
 * @author mpelzsherman
 */

class HashSet {

    private $_objects = array();

	/**
	 * @param  $o Object to be added to this set
	 * Returns: true if the set did not already contain the specified element.
	 */
	public function add($o) {
		foreach($this->_objects as $obj) {
			if ($obj == $o) {
				return false;
			}
		}
		$this->_objects[] = $o;
		return true;
	}

	/**
	 * @param  $o Object to be removed from this set
	 * Returns: true if the set contained the specified element.
	 */
	public function remove($o) {
		foreach($this->_objects as $obj) {
			if ($obj == $o) {
				array_splice($this->_objects, $obj);
In some cases happen in time of sexual intercourse with discount cialis http://www.glacialridgebyway.com/windows/Runestone%20County%20Park.html their partner. But ED problem put http://www.glacialridgebyway.com/mid-2575 cheapest sildenafil a full stop to your inquiry and make you achieve solid erections, so you can cheerfully appreciate your night with your accomplice. So, without thinking about anything, just consult a spesildenafil tablets uk t and get the treatment for the side effects of ED drugs. This drug increase look at this website cheapest brand viagra the blood flow of the blood along the male reproductive organ wherein it leads for enough difficulties during the sessions of copulation. 				return true;
			}
		}
		return false;
	}

	/**
	 * @param  $o
	 * Returns true if this set contains the specified element.
	 */
	public function contains($o) {
		foreach($this->_objects as $obj) {
			if ($obj == $o) {
				return true;
			}
		}
		return false;
	}

	public function size() {
		return count($this->_objects);
	}

	public function objects() {
		return $this->_objects;
	}

	/**
	 * Removes all of the elements from this set.
	 */
	public function clear() {
		$this->_objects = array();
	}
}

4 thoughts on “php implementation of Java HashSet

  1. According to the docs, === returns true if $a is equal to $b, and they are of the same type. So you could sacrifice some of the hip, dynamic nature of PHP this way, right? If I used ===, I might store the string “42” and the int 42 in the same set, which would be confusing, perhaps.

    Nope, I haven’t looked at SPL, and my boss hasn’t mentioned it either. Apparently he is of the opinion that the “native” functions are good enough, and that you should write the low-level code that best suits your needs. At first I was tempted to rebel against this, but the more I thought about it, it kind of makes sense. You could complain that PHP gives you too much flexibility here, but it also lets you fine-tune your code quite nicely to the problem at hand.

    I have to admit, after 3 months of PHP (with a lot of ExtJS as well), it’s kind of growing on me. I’ve learned a few tricks that are really helping, like “typing” variables with annotations. The SPL looks very cool; thanks for the tip!

  2. The SPL *is* native code – it’s been native in PHP5 for years. Given it’s Javaish nature, I thought you’d find it more to your liking. If memory serves, it’s not necessarily as fast as some of the ‘low-level’ functions it replaces, but may make the code more understandable, both to developers and to IDEs.

    The === issue – I thought you were trying to make sure the objects were the same one. The way you’re writing it, the data points could be anything, but you were using the word _objects, and I was assuming you’d only be putting objects in. The === would be used to ensure the objects were exactly the same or not.

    Good to hear PHP is growing on you, although I’m moving more towards Groovy/Grails still these days. Much of the dynamicness and flexibility of PHP, but (imo) somewhat cleaner syntax.

    The annotations typing – that’s just to help the IDE, right? It’s not affecting the code at runtime, or have you discovered some new trick I don’t know about?

  3. Thanks – I will ask my lead about SPL, but since he hasn’t mentioned it at all I suspect he’s not interested in using it. Funny that he didn’t mention it when I asked if there was anything like a PHP implementation of HashSet though.

    I suppose if I were to make this class more robust, I would use the === operator. But like my lead says, the choice of which operator to use usually depends on the context, so right there you’ve got a good case for not even trying to write a general-purpose class. I suppose I could mention that this is not an issue with strongly-typed languages, but that whole Java vs. PHP argument is getting rather old, isn’t it?

    Yes, the annotations typing only helps the IDE, but for me that is HUGE. When dealing with complex libraries, I really depend on code completion.

    PHP does seem to be gaining a bit more respect in the dev community at long last. It definitely is a big adjustment from Java, but I think it’s important as developers to be versatile since we often don’t get to choose our language.

Leave a Reply

Your email address will not be published. Required fields are marked *