Collections
From struts4php
The Collection is well known Interface in the JAVA programming language and provides a wide range of classes and interfaces with iterator algorithms. With PHP 4 there exists no possiblity to use objects in structures like for or foreach. This changes with PHP 5 and the Standard PHP Library SPL. The SPL brings the Iterator interface and this allows the usage of objects in for, foreach and while structures.
Contents |
The Collection interface
The Collection interface is the base interface for all classes providing iterator functionality. The collections package is built around the Collections interface and provides the container types ArrayList and HashMap.
The ArrayList is useful as a container without the possiblity to specify the key. If it is necessary to specifiy the keys the HashMap will be the right choice. Equal if an ArrayList or HashMap is used, each of them can stores all datatypes.
These classes implements the newly added SPL ArrayIterator interface and can be used in the foreach constructs as a object oriented replacement instead of the usual array.
The package also have a Set interface that defines a Dictionary class that uses objects as keys instead of flat datatypes like a HashMap or an ArrayList.
ArrayList
The ArrayList is an object oriented wrapper for an array. The ArrayList uses the SPL ArrayIterator interface to enable usage in foreach constructs. Every item in an ArrayList is available by it's key. The ArrayList does not allow an user to specify the keys by himself.
Example:
$list = new ArrayList(); $list->add("foo"); $list->add("bar"); foreach($list as $key => $item) { print $key . ":" . $item; } // prints // 0:foo // 1:bar
HashMap
The HashMap adds the possiblity to specify the index of the element to add. The specified index has to be a flat datatype like an integer, a string or a float. It is possible to mix several index types in one HashMap. If you add the same index two or more times, the newly added element overwrites the one added before.
Example:
$map = new HashMap(); $map->add(1, "foo"); $map->add("two", "bar"); foreach($map as $key => $item) { print $key . ":" . $item; } // prints // 1:foot // two:bar
TreeMap
The TreeMap object implements an automatically sorted HashMap. The sort order can be done with a Comparator, passed in the constructor, or automatically by the key.
Example without Comparator:
$map = new TreeMap(); $map->add(3, "foo"); $map->add(2, "bar"); foreach($map as $key => $item) { print $key . ":" . $item; } // prints // 2:bar // 3:foo
Example with Comparator:
class NameComparator() implements Comparator { public function compare($o1, $o2) { if($o1->getName() == $o2->getName()) { return 0; } if($o1->getName() > $o2->getName() { return 1; } if($o1->getName() < $o2->getName() { return -1; } } } class Person { private $name = null; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } } $map = new TreeMap(new NameComparator()); $map->add(1, new Person("foo"); $map->add(2, new Person("bar"); foreach($map as $key => $item) { print $key . ":" . $item; } // prints: // 2:bar // 1:foo
Dictionary
The Dictionary uses an object as key instead an integer or a string.
Example:
$dictionary = new Dictionary(); $dictionary->add(new Integer(1), new String("foo"));
