techdivision/collections

此包已被弃用,不再维护。未建议替代包。

提供基本PHP集合和工具的包。

0.1.1 2014-07-25 13:17 UTC

This package is auto-updated.

Last update: 2022-04-27 02:00:14 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Build StatusScrutinizer Code QualityCode Coverage

介绍

此包提供了一个通用的集合库。

该库基于PHP5的SPL扩展,并使用引入的迭代器。除了最常见的集合类型外,该库还提供了接口和异常。

安装

如果您想将此库用于应用程序,您可以通过在您的 composer.json 中添加以下内容来安装它:

{
    "require": {
        "techdivision/collections": "dev-master"
    },
}

并在您的项目中运行 composer update

使用

以下示例将向您简要介绍如何使用此库提供的类。

ArrayList

ArrayList将提供一个容器,可以存储不同数据类型的项。通过提供的方法,可以添加、返回或删除元素。ArrayList是无序的,有一个从0开始的持续整数索引。

// initialize a new ArrayList object 
$list = new ArrayList(); 
// add several values 
$list->add(new Integer(1)); 
$list->add(new Integer(2));
foreach($list as $key => $item) {
    echo "Found item with key " . $key . " value " . $item->__toString() . PHP_EOL; 
} 

// produces the following output 
Found item with key 0 and value 1 Found item with key 1 and value 2 

HashMap

HashMap将提供一个容器,可以存储不同数据类型的项。通过提供的方法,可以添加、返回或删除元素。HashMap与ArrayList一样无序,并支持所有简单数据类型作为索引。

// initialize a new HashMap object 
$map = new HashMap(); 

// add several values 
$map->add("number", new Integer(1)); 
$map->add("string", new String("foo")); 
foreach($map as $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

Dictionary类似于ArrayList或HashMap,将提供一个容器,可以存储不同数据类型的项。通过提供的方法,可以添加、返回或删除元素。Dictionary与ArrayList一样无序。但与ArrayList或HashMap不同,在简单数据类型之间,也允许使用对象作为索引。

// initialize a new Dictionary object 
$dictionary = new Dictionary(); 

// add several values 
$dictionary->add(new Integer(1), new Integer(12)); 
$dictionary->add(new String("foo"), new String("foo")); 

foreach($dictionary as $key => $item) {
    echo "Found item with key " . $key->__toString() . " and value " . $item->__toString() . PHP_EOL;
}

// produces the following output
Found item with key 1 and value foo Found item with key foo and value 12

TreeMap

TreeMap的使用与HashMap非常相似。主要区别在于项要么按升序排序,要么按构造函数传递的Predicate排序。

// initialize a new TreeMap object 
$map = new TreeMap(); 

// add several values 
$map->add(2, new Integer(12)); 
$map->add(1, new String("foo")); 

foreach($map as $key => $item) {
    echo "Found item with key " . $key . " and value " . $item->__toString() . PHP_EOL;
}

// produces the following output
Found item with key 1 and value foo Found item with key 2 and value 12

以下示例给出了使用Comparator进行排序的TreeMap的实现示例。

/** 
 * A class TestComparator to sort TreeMap by value.
 */
class TestComparator implements Comparator
{
    
    public function compare($object1, $object2)
    { 
        
        if ($object1->intValue() < $object2->intValue()) { 
            return -1;
        } 
        
        if ($object1->intValue() > $object2->intValue()) { 
            return 1;
        }
        
        if ($object1->intValue() == $object2->intValue()) {
            return 0;
        }
    } 
} 

// initialize a new TreeMap object and pass the TestComparator to the constructor 
$map = new TreeMap(new TestComparator()); 

// add several values
$map->add(1, new Integer(14)); 
$map->add(2, new Integer(13)); 
$map->add(3, new Integer(15)); 

foreach($map as $key => $item) { 
    echo "Found item with key " . $key . " and value " . $item->__toString() . PHP_EOL;
}

// produces the following output
Found item with key 2 and value 13 Found item with key 1 and value 14 Found item with key 3 and value 15

外部链接