PHP code example of appserver-io / collections

1. Go to this page and download the library: Download appserver-io/collections library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

appserver-io / collections example snippets


// 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 

// 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

// 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

// 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

/** 
 * 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