Skip to content

Instantly share code, notes, and snippets.

@pcsanwald
Created June 27, 2012 19:51
Show Gist options
  • Save pcsanwald/3006378 to your computer and use it in GitHub Desktop.
Save pcsanwald/3006378 to your computer and use it in GitHub Desktop.
collections example
package com.paulsanwald.prototype;
import static org.junit.Assert.*;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
public class GuavaWalkthrough {
@Test
public void test() {
// generics on the left hand side only, cool!
Collection<Integer> numbers = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
numbers.add(i);
}
Collection<Integer> filteredNumbers = Collections2.filter(numbers, new IsEven());
System.out.println(filteredNumbers);
}
public class IsEven implements Predicate<Integer> {
@Override
public boolean apply(Integer compare) {
return compare % 2 == 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment