Guava collections -- FluentIterable
来源:互联网 发布:nginx代理静态文件夹 编辑:程序博客网 时间:2024/05/15 23:52
Guava全文介绍地址:Google Guava
这次主要介绍是的是com.google.common.collect.FluentIterable.做为Java Iterable API的扩展,通过不同方式来提供功能类似于Java 8强大的”Streams” 库(in java.util.stream)。
关键的不同点有:
1. 一个stream通常是一次消费的。当一次任何”终端操作”(findFirst()或iterator())被调用时,它会变得无效。尽管流实现并包含所有Iterable接口的方法,实际上它并没有真正这样做。所以为了避免上面的情况,我们需要避免repeat-iterability。
2. FluentIterable并没有提供Stream中许多功能,包括最小/最大,层次分明,减少,排序,非常强大的收集,内置支持并行流操作。
3. FluentIterable提供了一些”Stream”中并不包含的特性。
4. Streams提供了IntStream的变体,强烈推荐使用。
5. Streams是标准的Java库,不需要依赖第三方库(但是不兼容Java 7以及以前版本)
下面就介绍一下它的典型用法:
1、准备工作
1.基本类
@Data@EqualsAndHashCodepublic class Person { private String firstName; private String lastName; private int age; private String sex;}
2.准备基础数据
public class FluentIterablesTest { private Person person1; private Person person2; private Person person3; private Person person4; private Person person5; private Person person6; Collection<Person> personList; @Before public void setUp() { person1 = new Person("Wilma", "Flintstone", 30, "F"); person2 = new Person("Fred", "Flintstone", 32, "M"); person3 = new Person("Betty", "Rubble", 31, "F"); person4 = new Person("Barney", "Rubble", 33, "M"); person5 = new Person("Barney", "Rubble", 33, "M"); person6 = new Person("Barney", "Rubble", 33, "M"); personList = Lists.newArrayList(person1, person2, person3, person4, person5, person6); }}
1、过滤功能
@Test public void testFilter() throws Exception { Iterable<Person> personsFilteredByAge = FluentIterable.from(personList).filter(new Predicate<Person>() { @Override public boolean apply(Person input) { return input.getAge() > 31; } }); assertThat(Iterables.contains(personsFilteredByAge, person2), is(true)); assertThat(Iterables.contains(personsFilteredByAge, person4), is(true)); assertThat(Iterables.contains(personsFilteredByAge, person5), is(true)); assertThat(Iterables.contains(personsFilteredByAge, person6), is(true)); assertThat(Iterables.contains(personsFilteredByAge, person1), is(false)); assertThat(Iterables.contains(personsFilteredByAge, person3), is(false)); }
2、其中一个满足条件
@Test public void testAnyMatch() throws Exception { Predicate<Person> personOlderThan31Predicate = new Predicate<Person>() { @Override public boolean apply(Person input) { return input.getAge() > 31; } }; assertThat(FluentIterable.from(personList).anyMatch(personOlderThan31Predicate),is(true)); }
3、全部满足条件
@Test public void testAllMatch() throws Exception { Predicate<Person> personOlderThan25Predicate = new Predicate<Person>() { @Override public boolean apply(Person input) { return input.getAge() > 25; } }; assertThat(FluentIterable.from(personList).allMatch(personOlderThan25Predicate),is(true)); }
4、对包含对象转换
@Test public void testTransform() throws Exception { List<String> transformed = 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")); }
5、过滤相同的对象
观察足够仔细的小伙伴了,可能有点疑惑。为什么person4,person5,person6这3个对象是相等的呢?没有错,就是为了演示这个对象过滤相同对象这个功能。
@Test public void testFilterEqualObject() throws Exception { assertTrue(personList.size() == 6); Set<Person> persons = FluentIterable.from(personList).toSet(); assertTrue(persons.size() == 4); }
这个方法注意,包含的集合不能为空,否则为报NullPointException
.
一行代码解决,是不是很优雅。更多功能等待你的发现。
1 0
- Guava collections -- FluentIterable
- Guava ---- FluentIterable迭代器
- guava (四)--集合中的FluentIterable类
- Guava collections -- Immutable Collections
- Google Guava Collections
- Google Guava Collections
- Guava Collections使用介绍
- Guava collections -- Lists
- Guava collections -- Sets
- Guava collections -- Maps
- Guava collections -- Multimaps
- Guava collections -- BiMap
- Guava collections -- Table
- Guava collections -- Range
- Guava collections -- Ordering
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Dagger2 使用详解
- 自定义C++类注册到Lua环境中遇到的问题
- Button的点击事件(显隐)
- 国内的windows硬件检测软件算是很牛逼的吧!!!?
- uva 10201 Adventures in Moving - Part IV
- Guava collections -- FluentIterable
- 支付宝集成问题
- 会议 or 期刊
- 初识Jekyll
- 矩阵求导
- Java类的一般形式
- 深究Oracle的隔离级别
- 字符串转换为数值类型
- 缺少双引号的字符串转换成Json对象