Mais conteúdo relacionado
Semelhante a Guava - Elements of Functional Programming (20)
Mais de Anna Shymchenko (20)
Guava - Elements of Functional Programming
- 3. www.luxoft.com
Groovy
[2,3,5].collect {it * it}; // [4,9,25]
[50, 100, 200].find {it >= 100}; // 100
team.any {Member m -> m.gender == FEMALE};// true
["Ann", "Alex", "Andrey"].every {it.startsWith("A")};
//true
[3, 7, 10].max(); // 10
- 11. www.luxoft.com
Vip Person Predicate
public enum VipPersonPredicate implements Predicate<Person> {
INSTANCE;
@Override
public boolean apply(Person p) {
return p.getGender() == FEMALE || p.getAge() <= 16;
}
}
- 14. www.luxoft.com
FluentIterable
public List<Dto> getAllRecords() {
return FluentIterable
.from(repository.getEntities())
.transform(EntityToDtoFunc.INSTANCE)
.toList();
}
public List<Person> getVips (List<Person> source) {
return FluentIterable
.from(source)
.filter(VipPersonPredicate.INSTANCE)
.toList();
}
- 24. www.luxoft.com
Cache
public class ServiceImpl {
public Foo findFooByStr(key) {
fooCache.get(str);
}
private LoadingCache<Key, Graph> fooCache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(
new CacheLoader<String, Foo>() {
public Foo load(String str) throws AnyException {
// long term running code
return result;