Skip to content

Repository files navigation

PHP collection library

Latest Stable VersionTotal DownloadsLicenseBuild StatusScrutinizer Code QualityCode Coverage

Introduction

This package provides a generic collection library.

The library is based on the SPL extension of PHP5 and uses the introduced iterators. The library also provides, beside the most used collection types, interfaces and exceptions.

Issues

In order to bundle our efforts we would like to collect all issues regarding this package in the main project repository's issue tracker. Please reference the originating repository as the first element of the issue title e.g.: [appserver-io/<ORIGINATING_REPO>] A issue I am having

Usage

The following examples will give you a short introduction how you can use the classes provided by this library.

ArrayList

An ArrayList will provide a container that can store items of different data types. With the provided methods, elements can be added, returned oder deleted. The ArrayList is unsorted and has an ongoing integer index that starts with 0.

// initialize a new ArrayList object $list = new ArrayList(); // add several values $list->add(newInteger(1)); $list->add(newInteger(2));
foreach($listas$key => $item) {
echo"Found item with key " . $key . " value " . $item->__toString() . PHP_EOL; } // produces the following output 
Found item with key 0and value 1 Found item with key 1and value 2

HashMap

A HashMap will provide a container that can store items of different data types. With the provided methods, elements can be added, returned oder deleted. The HashMap is unsorted like an ArrayList and all simple data types are supported as index.

// initialize a new HashMap object $map = newHashMap(); // add several values $map->add("number", newInteger(1)); $map->add("string", newString("foo")); foreach($listas$key => $item) { echo"Found item with key " . $key . " and value " . $item->__toString() . PHP_EOL;
} // produces the following output 
Found item with key number and value 1 Found item with key string and value foo

Dictionary

A Dictionary, like an ArrayList or a HashMap, will provide a container that can store items of different data types. With the provided methods, elements can be added, returned oder deleted. The Dictionary is unsorted like an ArrayList. But in opposite to an ArrayList or a HashMap, between simple data types, also objects are allowed as index.

// initialize a new Dictionary object $dictionary = newDictionary(); // add several values $dictionary->add(newInteger(1), newInteger(12)); $dictionary->add(newString("foo"), newString("foo")); foreach($dictionaryas$key => $item) {
echo"Found item with key " . $key->__toString() . " and value " . $item->__toString() . PHP_EOL;
}
// produces the following output
Found item with key 1and value foo Found item with key foo and value 12

TreeMap

The usage of a TreeMap is very similar to a HashMap. The main difference is, that the items are either sorted ascending, or by the Predicate passed to the constructor.

// initialize a new TreeMap object $map = newTreeMap(); // add several values $map->add(2, newInteger(12)); $map->add(1, newString("foo")); foreach($mapas$key => $item) {
echo"Found item with key " . $key . " and value " . $item->__toString() . PHP_EOL;
}
// produces the following output
Found item with key 1and value foo Found item with key 2and value 12

The next example gives you an example implementation for using a TreeMap, sorted with a Comparator.

/**  * A class TestComparator to sort TreeMap by value. */class TestComparator implements Comparator
{
publicfunctioncompare($object1, $object2)
{ if ($object1->intValue() < $object2->intValue()) { return -1;
} if ($object1->intValue() > $object2->intValue()) { return1;
}
if ($object1->intValue() == $object2->intValue()) {
return0;
}
} } // initialize a new TreeMap object and pass the TestComparator to the constructor $map = newTreeMap(newTestComparator()); // add several values$map->add(1, newInteger(14)); $map->add(2, newInteger(13)); $map->add(3, newInteger(15)); foreach($mapas$key => $item) { echo"Found item with key " . $key . " and value " . $item->__toString() . PHP_EOL;
}
// produces the following output
Found item with key 2and value 13 Found item with key 1and value 14 Found item with key 3and value 15

External Links

About

Package that provides basic PHP collections and utilities

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages