My favorites | Sign in
Project Logo
                
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));

Comment by amartinsn, Feb 19, 2009

Hey Sam! Congrats for your project. Am already using it in production code. Just created a post on hamcrest-collection, if you find it useful to add it as a resource, feel free!

http://blog.m.artins.net/hamcrest-out-of-test-code/

Congrats!

Comment by danrbr, Apr 22, 2009

This project made me see how usefull Hamcrest is, and how much Java needs better ways of expressing intetion. This was such an influence, that I felt compelled to make a similar project, called Fluent Java, taking enumerations to Smalltalk's (ok more like Ruby's) level.

Good work!


Sign in to add a comment
Hosted by Google Code