Guava 集合工具类 Iterables
截至Guava 1.2版本,Iterables使用FluentIterable类进行了补充,它包装了一个Iterable实例,并对许多操作提供了”fluent”(链式调用)语法。
一、接口声明
以下是 com.google.common.collect.Iterables
接口的声明:
@GwtCompatible(emulated=true) public final class Iterables extends Object
二、接口方法
官方文档:https://google.github.io/guava/releases/27.0.1-jre/api/docs/com/google/common/collect/Iterables.html
修饰符和类型 | 方法描述 |
---|---|
static <T> boolean | addAll(Collection<T> addTo, Iterable<? extends T> elementsToAdd) 将iterable中的所有元素添加到集合中。 |
static <T> boolean | all(Iterable<T> iterable, Predicate<? super T> predicate) 如果iterable中的每个元素都满足 predicate,则返回true。. |
static <T> boolean | any(Iterable<T> iterable, Predicate<? super T> predicate) 如果iterable中的任何元素满足 predicate,则返回true. |
static <T>Iterable<T> | concat(Iterable<? extends Iterable<? extends T>> inputs) 将多个iterables组合成一个iterables. |
static <T>Iterable<T> | concat(Iterable<? extends T>... inputs) 将多个iterables组合成一个iterables. |
static <T>Iterable<T> | concat(Iterable<? extends T> a, Iterable<? extends T> b) 将两个iterables组合成一个iterable. |
static <T>Iterable<T> | concat(Iterable<? extends T> a, Iterable<? extends T> b, Iterable<? extends T>c) 将三个个iterables组合成一个iterable.. |
static <T>Iterable<T> | concat(Iterable<? extends T> a, Iterable<? extends T> b, Iterable<? extends T>c, Iterable<? extends T> d) 将四个iterables组合成一个iterable.. |
static <T>Iterable<T> | consumingIterable(Iterable<T> iterable) Returns a view of the supplied iterable that wraps each generated Iterator through Iterators.consumingIterator(Iterator). |
static boolean | contains(Iterable<?> iterable, @Nullable Object element) 是否迭代器包含元素. |
static <T>Iterable<T> | cycle(Iterable<T> iterable) 返回一个 Iterable,迭代器在iterable元素上无限循环. |
static <T>Iterable<T> | cycle(T... elements) 返回一个迭代器,其迭代器在提供的元素上无限循环. |
static boolean | elementsEqual(Iterable<?> iterable1, Iterable<?> iterable2) 确定两个迭代是否包含相同顺序的相等元素. |
static <T>Iterable<T> | filter(Iterable<?> unfiltered, Class<T> desiredType) 返回未过滤的视图,其中包含所有类型为desiredType的元素. |
static <T>Iterable<T> | filter(Iterable<T> unfiltered, Predicate<? super T> retainIfTrue) 返回未过滤的视图,其中包含满足输入predicate 的所有元素. |
static <T> T | find(Iterable<? extends T> iterable, Predicate<? super T> predicate, T defaultValue) 返回满足给定predicate的iterable中的第一个元素,如果没有找到则返回defaultValue. |
static <T> T | find(Iterable<T> iterable, Predicate<? super T> predicate) 返回iterable中满足给定predicate的第一个元素; 仅在已知存在此类元素时才使用此方法. |
static int | frequency(Iterable<?> iterable, @Nullable Object element) 返回指定iterable中与指定对象相等的元素数. |
static <T> T | get(Iterable<? extends T> iterable, int position, T defaultValue) 返回指定位置的元素,没有使用默认值. |
static <T> T | get(Iterable<T> iterable, int position) 返回指定位置的元素. |
static <T> T | getFirst(Iterable<? extends T> iterable, T defaultValue) 返回 iterable 的第一个元素,没有使用默认值. |
static <T> T | getLast(Iterable<? extends T> iterable, T defaultValue) 返回 iterable 的最后一个元素,没有使用默认值. |
static <T> T | getLast(Iterable<T> iterable) 返回iterable的最后一个元素. |
static <T> T | getOnlyElement(Iterable<? extends T> iterable, T defaultValue) 返回iterable中包含的单个元素,如果iterable为空,则返回defaultValue. |
static <T> T | getOnlyElement(Iterable<T> iterable) 返回iterable中包含的单个元素. |
static <T> int | indexOf(Iterable<T> iterable, Predicate<? super T> predicate) 返回满足提供的predicate的第一个元素的iterable中的索引,如果Iterable没有这样的元素,则返回-1. |
static boolean | isEmpty(Iterable<?> iterable) 确定给定的iterable是否不包含元素 |
static <T>Iterable<T> | limit(Iterable<T> iterable, int limitSize) 返回包含其第一个limitSize元素的iterable视图. |
static <T>Iterable<T> | mergeSorted(Iterable<? extends Iterable<? extends T>> iterables, Comparator<?super T> comparator) 返回所有给定iterables的合并内容的iterables. |
static <T>Iterable<List<T>> | paddedPartition(Iterable<T> iterable, int size) 将iterable划分为给定大小的不可修改的子列表,如果需要,使用空值填充最终的iterable. |
static <T>Iterable<List<T>> | partition(Iterable<T> iterable, int size) 将iterable划分为给定大小的不可修改的子列表(最终的可迭代可能更小). |
static boolean | removeAll(Iterable<?> removeFrom, Collection<?> elementsToRemove) 删除属于提供的集合的每个元素 |
static <T> boolean | removeIf(Iterable<T> removeFrom, Predicate<? super T> predicate) 删除满足提供的predicate的每个元素. |
static boolean | retainAll(Iterable<?> removeFrom, Collection<?> elementsToRetain) 删除不属于提供的集合的每个元素. |
static int | size(Iterable<?> iterable) 返回iterable中的元素数. |
static <T>Iterable<T> | skip(Iterable<T> iterable, int numberToSkip) 返回跳过其第一个numberToSkip元素的iterable视图. |
static <T> T[] | toArray(Iterable<? extends T> iterable, Class<T> type) 将iterable的元素复制到数组中. |
static String | toString(Iterable<?> iterable) 返回iterable的字符串表示形式,格式为[e1,e2,...,en](即与Arrays.toString(Iterables.toArray(iterable))相同). |
static <F,T>Iterable<T> | transform(Iterable<F> fromIterable, Function<? super F,? extends T> function) 返回一个视图,其中包含将函数应用于fromIterable的每个元素的结果. |
static <T>Optional<T> | tryFind(Iterable<T> iterable, Predicate<? super T> predicate) 返回一个Optional,其中包含iterable中满足给定predicate的第一个元素(如果存在这样的元素)。 |
static <E>Iterable<E> | unmodifiableIterable(ImmutableCollection<E> iterable) 已过时 |
static <T>Iterable<T> | unmodifiableIterable(Iterable<? extends T> iterable) 返回不可修改视图. |
三、测试类
package com.example.guava.collect; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.*; import org.junit.Test; import javax.annotation.Nullable; import java.util.*; import static com.google.common.collect.Lists.newArrayList; import static java.util.Arrays.asList; public class IterablesTest { @Test public void create() { Iterable<String> iterable1 = Collections.emptySet(); Iterable<Integer> iterable2 = () -> Arrays.asList(0, 1).iterator(); Iterator<Integer> iterator = Arrays.asList(0, 1).iterator(); Iterable<String> iterable3 = Sets.newHashSet("a", null, "b"); Iterable<String> iterable4 = Collections.singleton("a"); Iterable<String> iterable5 = Collections.singletonList("foo"); } @Test public void getOnlyElement() { Iterable<String> iterable = Collections.emptySet(); System.out.println(Iterables.getOnlyElement(iterable, "bar")); // bar } @Test public void and_all_find_tryFind() { List<String> list = newArrayList("cool", "pants"); Predicate<String> predicate = Predicates.equalTo("pants"); System.out.println(Iterables.any(list, predicate)); // true System.out.println(Iterables.all(list, predicate)); // false System.out.println(Iterables.find(list, predicate)); // pants System.out.println(Iterables.find(list, Predicates.equalTo("aaa"), "bbb")); // bbb System.out.println(Iterables.find(list, Predicates.alwaysFalse(), "bbb")); // bbb System.out.println(Iterables.find(list, Predicates.alwaysTrue(), "bbb")); // cool System.out.println(Iterables.tryFind(list, predicate)); // Optional.of(pants) System.out.println(Iterables.tryFind(list, Predicates.equalTo("aaa"))); // Optional.absent() System.out.println(Iterables.tryFind(list, Predicates.alwaysFalse())); // Optional.absent() System.out.println(Iterables.tryFind(list, Predicates.alwaysTrue())); // Optional.of(cool) } @Test public void filter() { List<Object> list = newArrayList("A", "B", "C", "A", null, 1, null, 2); Iterable<Object> iterable = Iterables.filter(list, Predicates.equalTo("A")); System.out.println(iterable); // [A, A] Iterable<Integer> filter = Iterables.filter(list, Integer.class); System.out.println(filter); // [1, 2] System.out.println(Iterables.filter(list, Predicates.notNull())); // [A, B, C, A, 1, 2] } @Test public void toArray() { Iterable<String> iterable = Collections.singletonList("a"); String[] array = Iterables.toArray(iterable, String.class); System.out.println(Arrays.toString(array)); // [a] } @Test public void transform() { List<String> input = asList("1", "2", "3"); Iterable<Integer> result = Iterables.transform(input, from -> Integer.valueOf(from)); List<Integer> actual = newArrayList(result); System.out.println(actual); // [1, 2, 3] List<String> input2 = asList("a", "b", "c"); Iterable<String> result2 = Iterables.transform(input2, from -> from.toUpperCase()); System.out.println(result2); // [A, B, C] } @Test public void cycle() { // 循环迭代 Iterable<String> cycle = Iterables.cycle("a", "b", "c"); System.out.println(cycle); // [a, b, c] (cycled) int i = 0; for (String str : cycle) { i++; if (i > 20) { break; } System.out.println(str); } } @Test public void concat() { List<Integer> list1 = newArrayList(1); List<Integer> list2 = newArrayList(4); List<List<Integer>> input = newArrayList(list1, list2); Iterable<Integer> result = Iterables.concat(input); System.out.println(result); // [1, 4] List<Integer> list3 = newArrayList(7, 8); List<Integer> list4 = newArrayList(9); List<Integer> list5 = newArrayList(10); result = Iterables.concat(list1, list2, list3, list4, list5); System.out.println(result); // [1, 4, 7, 8, 9, 10] } @Test public void concat2() { Iterable<Integer> iterable = asList(1, 2, 3); int n = 4; Iterable<Integer> repeated = Iterables.concat(Collections.nCopies(n, iterable)); System.out.println(repeated); // [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3] } @Test public void partition() { Iterable<Integer> source = Collections.emptySet(); Iterable<List<Integer>> partitions = Iterables.partition(source, 1); System.out.println(partitions); // [] System.out.println(Iterables.isEmpty(partitions)); // true List<Integer> list = asList(1, 2, 3, 4, 5); partitions = Iterables.partition(list, 2); System.out.println(partitions); // [[1, 2], [3, 4], [5]] System.out.println(Iterables.size(partitions)); // 3 } @Test public void paddedPartition() { List<Integer> list = asList(1, 2, 3, 4, 5); Iterable<List<Integer>> partitions = Iterables.paddedPartition(list, 2); System.out.println(partitions); // [[1, 2], [3, 4], [5, null]] System.out.println(Iterables.getLast(partitions)); // [5, null] System.out.println(Iterables.size(partitions)); // 3 } @Test public void addAll() { List<String> alreadyThere = newArrayList("already", "there"); List<String> freshlyAdded = newArrayList("freshly", "added"); boolean changed = Iterables.addAll(alreadyThere, freshlyAdded); System.out.println(changed); // true System.out.println(alreadyThere); // [already, there, freshly, added] System.out.println(freshlyAdded); // [freshly, added] } @Test public void elementsEqual(){ Iterable<?> a; Iterable<?> b; // A few elements. a = asList(4, 8, 15, 16, 23, 42); b = asList(4, 8, 15, 16, 23, 42); System.out.println(Iterables.elementsEqual(a, b)); // false // An element differs. a = asList(4, 8, 15, 12, 23, 42); b = asList(4, 8, 15, 16, 23, 42); System.out.println(Iterables.elementsEqual(a, b)); // false // null versus non-null. a = asList(4, 8, 15, null, 23, 42); b = asList(4, 8, 15, 16, 23, 42); System.out.println(Iterables.elementsEqual(a, b)); // false System.out.println(Iterables.elementsEqual(b, a)); // false // Different lengths. a = asList(4, 8, 15, 16, 23); b = asList(4, 8, 15, 16, 23, 42); System.out.println(Iterables.elementsEqual(a, b)); // false System.out.println(Iterables.elementsEqual(a, b)); // false } @Test public void limit() { Iterable<String> iterable = newArrayList("foo", "bar", "baz"); Iterable<String> limited = Iterables.limit(iterable, 2); System.out.println(limited); // [foo, bar] } @Test public void skip() { Collection<String> set = ImmutableSet.of("a", "b", "c", "d", "e"); System.out.println(Iterables.skip(set, 2)); // [c, d, e] System.out.println(Iterables.skip(set, 20)); } @Test public void get() { ArrayList<String> list = newArrayList("a", "b", "c"); System.out.println(Iterables.get(list, 1, "d")); // b System.out.println(Iterables.get(list, 5, "d")); // d } @Test public void getFirst_getLast() { Iterable<String> iterable = Collections.emptyList(); System.out.println(Iterables.getFirst(iterable,null)); // null System.out.println(Iterables.getFirst(iterable, "bar")); // bar iterable = asList("foo", "bar"); System.out.println(Iterables.getFirst(iterable,null)); // foo System.out.println(Iterables.getLast(iterable)); // bar System.out.println(Iterables.getLast(iterable,"xxx")); // bar } @Test public void unmodifiableIterable() { List<String> list = newArrayList("a", "b", "c"); Iterable<String> iterable = Iterables.unmodifiableIterable(list); Iterable<String> iterable2 = Iterables.unmodifiableIterable(iterable); System.out.println(iterable); // [a, b, c] System.out.println(iterable2); // [a, b, c] } @Test public void frequency() { Multiset<String> multiset = ImmutableMultiset.of("a", "b", "a", "c", "b", "a"); Set<String> set = Sets.newHashSet("a", "b", "c"); List<String> list = newArrayList("a", "b", "a", "c", "b", "a"); System.out.println(Iterables.frequency(multiset, "a")); // 3 System.out.println(Iterables.frequency(multiset, "b")); // 2 System.out.println(Iterables.frequency(multiset, "d")); // 0 System.out.println(Iterables.frequency(multiset, 1.2)); // 0 System.out.println(Iterables.frequency(multiset, null)); // 0 } @Test public void removeAll() { List<String> list = newArrayList("a", "b", "c", "d", "e"); System.out.println(Iterables.removeAll(list, newArrayList("b", "d", "f"))); // true System.out.println(list); // [a, c, e] System.out.println(Iterables.removeAll(list, newArrayList("x", "y", "z"))); // false System.out.println(list); // [a, c, e] } @Test public void retainAll() { List<String> list = newArrayList("a", "b", "c", "d", "e"); System.out.println(Iterables.retainAll(list, newArrayList("b", "d", "f"))); // true System.out.println(list); // [b, d] System.out.println(Iterables.retainAll(list, newArrayList("b", "e", "d"))); // false System.out.println(list); // [b, d] } @Test public void removeIf() { List<String> list = newArrayList("a", "b", "c", "d", "e"); boolean removeIf = Iterables.removeIf(list, new Predicate<String>() { @Override public boolean apply(@Nullable String input) { return input.equals("b") || input.equals("s"); } }); System.out.println(removeIf); // true } }
四、相关文章
未经允许请勿转载:程序喵 » Google Guava 快速入门 —— 【集合工具】Iterables 类