1. FluentIterable
FluentIterable 流迭代器早于JDK8的stream产生,提了了一些集合类的过滤、转换等相关操作。在JDK8之前使用比较多,JDK8之后推荐使用JDK的stream相关操作。FluentIterable的相关操作可以作为了解。
package org.example.model.guava;import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import org.assertj.core.util.Lists;
import org.junit.Test;import java.util.ArrayList;import static org.assertj.core.api.Assertions.assertThat;public class FluentIterableTest {@Testpublic void test1() {ArrayList<String> strings = Lists.newArrayList("A", "B", "C", "DDD");FluentIterable<String> fit = FluentIterable.from(strings);assertThat(fit.size()).isEqualTo(4); // size操作不建议使用,可以需要遍历计算长度// filterFluentIterable<String> filter = fit.filter(e -> e != null && e.length() > 2);assertThat(filter.size()).isEqualTo(1);assertThat(strings).size().isEqualTo(4); // filter操作不会影响原始列表assertThat(fit.size()).isEqualTo(4); // filter为新对象,不影响旧的fitassertThat(fit.contains("A")).isTrue();// 使用append添加元素,产生新的FluentIterableFluentIterable<String> append = fit.append("E");assertThat(append.size()).isEqualTo(5);assertThat(append.contains("E")).isTrue();assertThat(fit.size()).isEqualTo(4); // 不影响旧的fitassertThat(strings).size().isEqualTo(4); // 不影响元素list}@Testpublic void test2() {ArrayList<String> strings = Lists.newArrayList("A", "B", "C", "DDD");FluentIterable<String> fit = FluentIterable.from(strings);assertThat(fit.size()).isEqualTo(4); // size操作不建议使用,可以需要遍历计算长度assertThat(fit.allMatch(e -> e.equals("A"))).isFalse();assertThat(fit.allMatch(e -> e.equals("B"))).isFalse();Optional<String> firstMatch = fit.firstMatch(e -> e.equals("B"));assertThat(firstMatch.isPresent()).isTrue(); // 注意这里guava的OptionalOptional<String> first = fit.first();assertThat(first.isPresent()).isTrue(); // 注意这里guava的OptionalOptional<String> last = fit.last();assertThat(last.isPresent()).isTrue(); // 注意这里guava的OptionalFluentIterable<String> limit = fit.limit(3);assertThat(fit.size()).isEqualTo(4); // limit会创建新的FluentIterablelimit.forEach(System.out::print); // ABC// toList,toMap转换为list和mapImmutableList<String> list = fit.toList();ArrayList<String> list2 = Lists.newArrayList("F");fit.copyInto(list2); // 将数据拷贝到list2System.out.println(list2); // [F, A, B, C, DDD]// 转换System.out.println(fit.transform(String::length).toList()); // [1, 1, 1, 3]}
}
2. Lists
package org.example.model.guava;import com.google.common.collect.Lists;
import org.junit.Test;import java.util.ArrayList;
import java.util.List;public class ListsTest {@Testpublic void test1() {// 笛卡尔乘积List<List<String>> lists = Lists.cartesianProduct(Lists.newArrayList("1", "2"), Lists.newArrayList("A", "B"));System.out.println(lists); // [[1, A], [1, B], [2, A], [2, B]]// 转换ArrayList<String> strings = Lists.newArrayList("A", "B", "C");List<String> transform = Lists.transform(strings, String::toLowerCase); // 入参不能为空System.out.println(transform); // [a, b, c]// 反转System.out.println(Lists.reverse(strings)); // [C, B, A]// 拆分遍历System.out.println(Lists.partition(strings, 2)); // [[A, B], [C]]}
}