十二、集合操作

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 {
    @Test
    public void test1() {
        ArrayList strings = Lists.newArrayList("A", "B", "C", "DDD");
        FluentIterable fit = FluentIterable.from(strings);
        assertThat(fit.size()).isEqualTo(4); // size操作不建议使用,可以需要遍历计算长度

        // filter
        FluentIterable 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为新对象,不影响旧的fit
        assertThat(fit.contains("A")).isTrue();


        // 使用append添加元素,产生新的FluentIterable
        FluentIterable append = fit.append("E");
        assertThat(append.size()).isEqualTo(5);
        assertThat(append.contains("E")).isTrue();
        assertThat(fit.size()).isEqualTo(4); // 不影响旧的fit
        assertThat(strings).size().isEqualTo(4); // 不影响原始list
    }

    @Test
    public void test2() {
        ArrayList strings = Lists.newArrayList("A", "B", "C", "DDD");
        FluentIterable 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 firstMatch = fit.firstMatch(e -> e.equals("B"));
        assertThat(firstMatch.isPresent()).isTrue(); // 注意这里guava的Optional

        Optional first = fit.first();
        assertThat(first.isPresent()).isTrue(); // 注意这里guava的Optional

        Optional last = fit.last();
        assertThat(last.isPresent()).isTrue(); // 注意这里guava的Optional

        FluentIterable limit = fit.limit(3);
        assertThat(fit.size()).isEqualTo(4); // limit会创建新的FluentIterable
        limit.forEach(System.out::print); // ABC

        // toList,toMap转换为list和map
        ImmutableList list = fit.toList();

        ArrayList list2 = Lists.newArrayList("F");
        fit.copyInto(list2); // 将数据拷贝到list2
        System.out.println(list2); // [F, A, B, C, DDD]

        // 转换
        System.out.println(fit.transform(String::length).toList()); // [1, 1, 1, 3]
    }
}

2. Lists

import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 功能描述
 *
 * @author q00811349
 * @since 2023-09-12
 */
public class ListsTest {
    @Test
    public void test1() {
        // 笛卡尔乘积
        List> lists = Lists.cartesianProduct(Lists.newArrayList("1", "2"), Lists.newArrayList("A", "B"));
        System.out.println(lists); // [[1, A], [1, B], [2, A], [2, B]]

        // 转换
        ArrayList strings = Lists.newArrayList("A", "B", "C");
        List 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]]

        // 创建一个LinkedList, 支持创建空的和使用迭代器创建,不支持使用给定元素创建
        LinkedList linkedList = Lists.newLinkedList(strings);
        System.out.println(linkedList); //  [A, B, C]
        
        // 创建copyOnWriteArrayList, 高并发读多写少场景使用
        CopyOnWriteArrayList copyOnWriteArrayList = Lists.newCopyOnWriteArrayList(linkedList);
        System.out.println(copyOnWriteArrayList); //  [A, B, C]
    }
}

你可能感兴趣的:(java)