Guava库学习:学习Collections(一)FluentIterable

    链接地址:http://www.xx566.com/detail/137.html

    在Java开发中,java.util.Collections包含的工具方法经常需要用到,在Guava 中,com.google.common.collect提供了更多的使用于所有集合的静态工具方法,使我们能够更简便的对集合进行系列的操 作,Guava Collections能够让我们的代码变得更加的简洁,更具可读性,本篇我们就来学习一下FluentIterable。

    

    FluentIterable类提供了一个强大的接口,通过一种链式调用的编程风格,来处理Iterable实例。这种链式调用风格使我们可以链式的调用 所需的方法,让我们的代码更具可读性。翻开FluentIterable的源码,我们看到,FluentIterable是一个抽象类同时实现了 Iterable接口,内部提供了一个静态的from方法,用于接收一个Iterable接口的实现作为参数,返回一个包装了Iterable接口的 FluentIterable实例。FluentIterable可以链式调用toList、toSet、 toMap、 toSortedList以及toSortedSet等方法,转换为我们熟悉的集合类型。 

    

    通过源码我们看到,除了类型的转换,FluentIterable实例也包含多种处理集合的方法,这里不再依次的进行学习,我们只针对以下两个方法进行学 习和说明,FluentIterable.filter和FluentIterable.transform方法,顾名思义,分别用于集合的过滤和转换, 我们先简单的做一下解释,如下:

filter(Predicate<? super E> predicate):接收一个Predicate作为参数,过滤那些能够满足predicate的元素,返回FluentIterable实例。

transform(Function<? super E, T> function):接收一个Function作为参数,相应转换集合中的元素,返回FluentIterable实例。

更多Function,请参阅:Guava库学习:函数编程(一)使用Function和Functions进行对象转换

更多Predicate,请参阅:Guava库学习:函数编程(二)使用Predicate和Predicates进行对象过滤

 

    接下来,我们来看下面的代码,通过代码我们能够更好的学习FluentIterable,代码如下:

package guava;
 
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
 
import java.util.List;
 
/**
 * FluentIterable:处理Iterable,链式风格调用
 * User: Realfighter
 * Date: 2014/8/29
 * Time: 20:14
 */
public class FluentIterableTest {
 
    //测试用Girl对象
    static class Girl {
        int age;
        String face;
 
        Girl(int age, String face) {
            this.age = age;
            this.face = face;
        }
 
        public int getAge() {
            return age;
        }
 
        public void setAge(int age) {
            this.age = age;
        }
 
        public String getFace() {
            return face;
        }
 
        public void setFace(String face) {
            this.face = face;
        }
    }
 
    private List<Girl> girls;//测试用对象集合
 
    //这里初始化一些测试数据
    @Before
    public void setUp() {
        Girl girl1 = new Girl(17, "nice");
        Girl girl2 = new Girl(18, "not so nice");
        Girl girl3 = new Girl(19, "nice");
        Girl girl4 = new Girl(20, "not so nice");
        //这里使用Lists.newArrayList添加对象,避免多次调用list.add方法,下篇会有说明
        girls = Lists.newArrayList(girl1, girl2, girl3, girl4);
    }
 
    @Test
    public void testFluentIterable() {
        /**
         * from方法:用于包装Iterable接口,返回FluentIterable实例
         * filter方法:用于过滤集合中元素,返回过滤后的集合
         */
        Iterable<Girl> iterable = FluentIterable.from(girls).filter(new Predicate<Girl>() {
            @Override
            public boolean apply(Girl input) {
                //过滤相貌nice的Girl对象
                return "nice".equals(input.getFace());
            }
        });
        for (Girl girl : iterable) {
            /**打印结果:
             17
             19
             */
            System.out.println(girl.getAge());
        }
        /**
         * transform方法:用于根据指定Function转换集合元素,返回转换后的集合
         */
        Iterable<String> fluentIterable = FluentIterable.from(girls).transform(new Function<Girl, String>() {
            @Override
            public String apply(Girl input) {
                //Joiner类对相应的元素内容进行拼接处理
                return Joiner.on(",").join(input.getAge(), input.getFace());
            }
        });
        for (String girl : fluentIterable) {
            /** 打印结果:
             17,nice
             18,not so nice
             19,nice
             20,not so nice
             */
            System.out.println(girl);
        }
    }
 
}

转载于:https://my.oschina.net/realfighter/blog/349899

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值