appserver-io / collections
提供基本PHP集合和实用工具的包。
2.0.0
2017-06-07 07:50 UTC
Requires
- php: >=5.4.0 < 8.0
- appserver-io/lang: ~4.0
Requires (Dev)
- appserver-io/build: ~1.0
README
简介
此包提供了一个通用的集合库。
该库基于PHP5的SPL扩展,并使用引入的迭代器。除了最常用的集合类型之外,该库还提供了接口和异常。
问题
为了整合我们的努力,我们希望将有关此包的所有问题收集到 主项目仓库的问题跟踪器 中。请将原始仓库作为问题标题的第一个元素,例如:[appserver-io<ORIGINATING_REPO>] 我遇到的问题
使用方法
以下示例将简要介绍您如何使用此库提供的类。
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($list 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
外部链接
- 在 appserver.io 的文档