搭建一个视频网站多少钱,邢台市网络公司,怎样提升企业网站的访问,公司门户网站的设计与实现1. FluentIterable
FluentIterable 流迭代器早于JDK8的stream产生#xff0c;提了了一些集合类的过滤、转换等相关操作。在JDK8之前使用比较多#xff0c;JDK8之后推荐使用JDK的stream相关操作。FluentIterable的相关操作可以作为了解。
package org.example.model.guava;i…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() {ArrayListString strings Lists.newArrayList(A, B, C, DDD);FluentIterableString fit FluentIterable.from(strings);assertThat(fit.size()).isEqualTo(4); // size操作不建议使用可以需要遍历计算长度// filterFluentIterableString 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添加元素产生新的FluentIterableFluentIterableString 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() {ArrayListString strings Lists.newArrayList(A, B, C, DDD);FluentIterableString 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();OptionalString firstMatch fit.firstMatch(e - e.equals(B));assertThat(firstMatch.isPresent()).isTrue(); // 注意这里guava的OptionalOptionalString first fit.first();assertThat(first.isPresent()).isTrue(); // 注意这里guava的OptionalOptionalString last fit.last();assertThat(last.isPresent()).isTrue(); // 注意这里guava的OptionalFluentIterableString limit fit.limit(3);assertThat(fit.size()).isEqualTo(4); // limit会创建新的FluentIterablelimit.forEach(System.out::print); // ABC// toListtoMap转换为list和mapImmutableListString list fit.toList();ArrayListString 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() {// 笛卡尔乘积ListListString lists Lists.cartesianProduct(Lists.newArrayList(1, 2), Lists.newArrayList(A, B));System.out.println(lists); // [[1, A], [1, B], [2, A], [2, B]]// 转换ArrayListString strings Lists.newArrayList(A, B, C);ListString 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]]}
}