Iterables



filter


import com.google.common.base.Function;
import com.google.common.base.Predicate;

import static com.google.common.collect.Iterables.*;
import static com.google.common.collect.Lists.*;

Iterable ls = newArrayList("google guava", "is", "rock");
ls = filter(ls, new Predicate<String>() {
    @Override
    public boolean apply(String s){
        return s.length >= 2;
    }
});

for(String s: ls) {
    System.out.println(s);
}


transform


Iterable ls = newArrayList("google guava", "is", "rock");
ls = transform(ls, new Function<String, String>() {
    @Override
    public String apply(String s) {
return s + "/";
    }
});

for(String s: ls) {
    System.out.println(s);
}



Scala Version

filter
List("google guava", "is", "rock").filter(_>2)

map
List("google guava", "is", "rock").map(_ + "/")

Comments