Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's HashSet equivalent in PHP

I'm relatively new to php and having a hard time figuring out the right data structure to use. Let's say I have a class FooBar with equals() and hashCode() properly implemented. What kind of collection in php (if there is any at all) that most resembles Java's hashSet? I need a collection of objects without duplicates. Someone's suggested using array and the function array_key_exists, but I was just wondering if there's another way to do this?

like image 494
0x56794E Avatar asked May 16 '12 06:05

0x56794E


People also ask

Where is HashSet used in real time?

HashSets are excellent when you want to store large number of collections. Because, the basic add, remove, contains operations are constant time operations. In other words, putting an object in an empty set is same as putting it in a set having n records.

How do you compare two hash sets?

The equals() method of java. util. HashSet class is used verify the equality of an Object with a HashSet and compare them. The list returns true only if both HashSet contains same elements, irrespective of order.

Does HashSet use hashing?

HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage. A hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code.


2 Answers

Starting from release 5.2 Php offers SplObjectStorage, that offers functionalities of Java's Set:

  • Cares about uniqueness (same object can't be added twice)
  • Easy to iterate through the collection
  • Easy to check existence of an object in the collection

Check http://technosophos.com/content/set-objects-php-arrays-vs-splobjectstorage for example of use

like image 152
ab_dev86 Avatar answered Oct 11 '22 17:10

ab_dev86


There are few Data structures available in the PHP programming language provided by the Standard PHP Library (SPL). Although they are nothing when compared with the Java Collections Framework implementations, sometimes they can be very useful by providing a more advanced functionality than that of the arrays. You can find the documentation of the available Data structures here.

The most HashSet-like Data structure in PHP would be SplObjectStorage.

From the documentation:

The SplObjectStorage class provides a map from objects to data or, by ignoring data, an object set. This dual purpose can be useful in many cases involving the need to uniquely identify objects.

Objects in PHP don't implement the equals() and the hashCode() methods. Uniqueness of objects is determined by the value returned from the spl_object_hash() function. The same value is used by the SplObjectStorage class to uniquely identify the objects it contains. The SplObjectStorage::getHash($object) method can be used to retrieve the identifier of an object contained in the SplObjectStorage collection.

like image 26
zafarkhaja Avatar answered Oct 11 '22 19:10

zafarkhaja