I often use Map<U,V> that I transform and filter to get another Map<U,V'>, but the transformation is horribly verbose, I saw FluentIterable class in the quesion Guava: how to combine filter and transform?, is is possible to use it to simplify the following ?

public class GuavaTest {

    public static class A{
        int a;
        int b;
        int c;
        public A(int a, int b, int c){
            this.a=a; this.b=b; this.c=c;
        }
    }

    public static void main(String[] args) {

        //Example 2
        final Map<String, A> map =Maps.newHashMap();

        map.put("obj1",new A(1,1,1)); map.put("obj2",new A(1,2,1));
        map.put("obj3",new A(1,3,1)); map.put("obj4",new A(1,4,1));

        Function<A, Integer> function = new Function<A, Integer>() {
            @Nullable
            @Override
            public Integer apply(@Nullable A input) {
                return input.b;
            }
        };

        Predicate<Integer> isPair = new Predicate<Integer>() {
            @Override
            public boolean apply(@Nullable Integer input) {
                return input % 2 == 0;
            }
        };

        //Can I use FluentIterable here ??
        Map<String, Integer> stringIntegerMap = Maps.transformValues(map, function);
        Map<String, Integer> stringIntegerMap1 = Maps.filterValues(stringIntegerMap, isPair);
    }
}
有帮助吗?

解决方案

No.

FluentIterable doesn't really do much of anything with maps, and certainly won't shorten your Function and Predicate implementations. The only thing that will shorten those is Java 8 lambdas, really.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top