Mango provides a number of generic algorithms

The algorithms are provided as static methods on the Algorithms class.

count

int count(java.util.Collection collection, Object value);
int count(java.util.Collection collection, int start, int end, Object value);
int count(java.util.List list, int start, int end, Object value);
int count(java.util.Iterator iterator, Object value);

count computes the number of elements in the sequence that are equal to value (value may be null). The objects in the sequence and value must be comparable using Object.equals (unless value is null).

countIf

int countIf(java.util.Collection collection, Predicate test);
int countIf(java.util.Collection collection, int start, int end, Predicate test);
int countIf(java.util.List list, int start, int end, Predicate test);
int countIf(java.util.Iterator iterator, Predicate test);

countIf is similar to count, but more general. It computes the number of elements in the sequence which satisfy some condition. The condition is a described in the user-supplied test object, and countIf computes the number of objects for which test.test(o) is true.

countIfNot

int countIfNot(java.util.Collection collection, Predicate test);
int countIfNot(java.util.Collection collection, int start, int end, Predicate test);
int countIfNot(java.util.List list, int start, int end, Predicate test);
int countIfNot(java.util.Iterator iterator, Predicate test);

CountIfNot is the complement of CountIf. It counts the number of elements in the sequence which fail some condition. The condition is a described in the user-supplied test object, and CountIfNot computes the number of objects such that test.test(o) is false.

find

Object find(java.util.Collection collection, Object value);
Object find(java.util.Collection collection, int start, int end, Object value);
Object find(java.util.List list, int start, int end, Object value);
Object find(java.util.Iterator iterator, Object value);

Searchs the sequence for the given value. Returns the Object, or null if the value is not found. Any iterator will have been advanced to the next object in the sequence. The objects in the sequence and value must be comparable using Object.equals (unless value is null).

findIf

Object findIf(java.util.Collection collection, Predicate test);
Object findIf(java.util.Collection collection, int start, int end, Predicate test);
Object findIf(java.util.List list, int start, int end, Predicate test);
Object findIf(java.util.Iterator iterator, Predicate test);

Searchs the sequence and returns the first object encountered for which the Predicate returns true. Returns the Object, or null if the value is not found. Any iterator will have been advanced to the next object in the sequence.

findIfNot

Object findIfNot(java.util.Collection collection, Predicate test);
Object findIfNot(java.util.Collection collection, int start, int end, Predicate test);
Object findIfNot(java.util.List list, int start, int end, Predicate test);
Object findIfNot(java.util.Iterator iterator, Predicate test);

Searchs the sequence traversed by the Iterator and returns the first object encountered for which the Predicate returns false. The iterator will have been advanced to the next object in the sequence.

forEach

void forEach(java.util.Collection collection, UnaryFunction fn);
void forEach(java.util.Collection collection, int start, int end, UnaryFunction fn);
void forEach(java.util.List list, int start, int end, UnaryFunction fn);
void forEach(java.util.Iterator iterator, UnaryFunction fn);

The algorithm ForEach applies the function fn to each element in the sequence. E.g. to print all the elements in a list

Mango.forEach(list, Bind.Method(System.out, "println"));
is equivalent to
for(int i = 0; i < list.size(); ++i)
    System.out.println(list.get(i));

remove

void remove(java.util.Collection collection, Object value);
void remove(java.util.Collection collection, int start, int end, Object value);
void remove(java.util.List list, int start, int end, Object value);
void remove(java.util.Iterator iterator, Object value);

The remove algorithm traverses the sequence, and removes any objects which equal value.

removeIf

void removeIf(java.util.Collection collection, Predicate test);
void removeIf(java.util.Collection collection, int start, int end, Predicate test);
void removeIf(java.util.List list, int start, int end, Predicate test);
void removeIf(java.util.Iterator iterator, Predicate test);

The removeIf algorithm traverses the sequence, and removes any objects for which test is true.

transform

void transform(java.util.Collection collection, UnaryFunction fn, java.util.Collection results);
void transform(java.util.Collection collection, int start, int end, UnaryFunction fn, java.util.Collection results);
void transform(java.util.List list, int start, int end, UnaryFunction fn, java.util.Collection results);
void transform(java.util.Iterator iterator, UnaryFunction fn, java.util.Collection results);

The algorithm transform applies the function fn to each element in the iterator sequence. The return value of fn is appended to the results collection. If the return value of fn is itself collection, then each member of that collection is added to results.


Jez Higgins