guava_ch

Using the FluentIterable.transform method

FluentIterable.transform方法是一种映射操作,其中每个元素都应用Function。这个操作的输出一个新的与原始长度相同的由转换后对象组成的iterable,这可能会转移任意或所有的原始元素。下面就举例transform方法,再次使用上面例子中的数据。

@Test
public void testTransform() throws Exception {
    List<String> transformedPersonList =
    FluentIterable.from(personList).transform(new Function<Person,String>() {
        @Override
        public String apply(Person input) {
            return Joiner.on('#').join(input.getLastName(),input.getFirstName(), input.getAge());

        }).toList();

        assertThat(transformed.get(1), is("Flintstone#Fred#32"));
}

这个例子中,我们转换每一个personList中的元素为以#隔开的last name,first name和Person对象组成的字符串。 首先使用FluentIterable.from方法,然后传递Functiontransform用于串联。当最后,我们同样串联了第三个方法,toList,返回最后的结果为List<String>。同样还有toSet , toMap , toSortedListtoSortedSet方法可以用。toMap方法以FluentIterable中的元素为key,需要Function来提value。 toSortedListtoSortedSet使用一个Comparator来指定顺序。