What's new? | Help | Directory | Sign in
Google
                
Search
for
Updated Aug 09, 2007 by sam.newman
Labels: Phase-Deploy, Featured
GettingStarted  
A Quick Guide on how to use hamcrest-collections

Download It

Grab the zip, unzip it and add the Jar to your classpath.

Runtime Dependencies

Hamcrest-collections relies on Hamcrest 1.1 or later.

Use It

Select & Reject

select and reject allow you to filter lists based on any Hamcrest Matcher. Hamcrest comes with useful Matchers like greatherThan, equalTo etc., but you are free to create your own.

select returns a collection of items that match the matcher, reject returns a list of items that don't match the matcher. For example:

import static org.hamcrest.Matchers greaterThan;
import static org.hamcrestcollections.SelectMatcher select;
import static org.hamcrestcollections.RejectMatcher reject;

...

List<Integer> myInts = new ArrayList<Integer>();
myInts.add(1);
myInts.add(2);
myInts.add(3);
myInts.add(4);

Iterable<Integer> oneAndTwo = reject(myInts, greaterThan(2));
Iterable<Integer> threeAndFour = select(myInts, greaterThan(2));

Sign in to add a comment