Do not use loop for list iterations - lamdaj 본문

Programming/Java

Do not use loop for list iterations - lamdaj

halatha 2011. 4. 29. 05:42
2011/04/26 - [Programming/Java] - Do not use loops for list operations
2011/04/28 - [Programming/Java] - Do not use loops for list operations
import java.util.ArrayList;
import java.util.List;
import com.google.common.collect.Lists;
import ch.lambdaj.Lambda;
import static org.hamcrest.Matchers.greaterThan;

//	item 3 from http://codemonkeyism.com/generation-java-programming-style
//	-cp .:./Guava/guava-r09/guava-r09.jar:./lambdaj-2.3.2-with-dependencies.jar
public class TestFilter	{
	public static void main(final String[] args)	{
		List<Person>	persons	=	Lists.newArrayList(
										new Person("A", 15),
										new Person("B", 16),
										new Person("C", 17));
		List<Person>	beerDrinkers	=
			//	http://code.google.com/p/lambdaj
			Lambda.select(persons,
				Lambda.having(Lambda.on(Person.class).getAge(), greaterThan(16)));
	}
}

interface HasAge	{
	public int getAge();
}

class Person implements HasAge	{
	public final String	name;
	public final int	age;
	public Person(String name, int age)
	{
		this.name	=	name;
		this.age	=	age;
	}
	public int getAge()	{	return this.age;	}
	public String toString(){	return	name + " = " + age;	}
}
Comments