Guava库中的`ImmutableCollections`进行集合操作的示例

Guava库中的ImmutableCollections进行集合操作的示例,包括创建不可变集合和常见的集合操作:

1. 创建不可变集合

使用ImmutableSet创建不可变集合
java 复制代码
import com.google.common.collect.ImmutableSet;

public class ImmutableSetExample {
    public static void main(String[] args) {
        ImmutableSet<String> fruits = ImmutableSet.of("apple", "banana", "cherry");
        System.out.println("ImmutableSet: " + fruits);
    }
}
使用ImmutableList创建不可变列表
java 复制代码
import com.google.common.collect.ImmutableList;

public class ImmutableListExample {
    public static void main(String[] args) {
        ImmutableList<String> colors = ImmutableList.of("red", "green", "blue");
        System.out.println("ImmutableList: " + colors);
    }
}
使用ImmutableMap创建不可变映射
java 复制代码
import com.google.common.collect.ImmutableMap;

public class ImmutableMapExample {
    public static void main(String[] args) {
        ImmutableMap<String, Integer> numberMap = ImmutableMap.of("one", 1, "two", 2, "three", 3);
        System.out.println("ImmutableMap: " + numberMap);
    }
}

2. 使用Builder构建不可变集合

java 复制代码
import com.google.common.collect.ImmutableSet;

public class ImmutableBuilderExample {
    public static void main(String[] args) {
        ImmutableSet<String> set = ImmutableSet.<String>builder()
                .add("apple")
                .add("banana")
                .add("cherry")
                .build();
        System.out.println("ImmutableSet built with builder: " + set);
    }
}

3. 集合操作示例

过滤和转换

使用Guava的FluentIterable进行集合的过滤和转换:

java 复制代码
import com.google.common.collect.FluentIterable;
import com.google.common.base.Predicate;

import java.util.Arrays;
import java.util.List;

public class FilterExample {
    public static void main(String[] args) {
        List<String> strings = Arrays.asList("one", "two", "three", "four");
        Predicate<String> startsWithT = input -> input.startsWith("t");
        List<String> filtered = FluentIterable.from(strings).filter(startsWithT).toList();
        System.out.println("Filtered list: " + filtered); // 输出: [two, three]
    }
}
集合的交集、差集和并集

使用Guava的Sets类进行集合的交集、差集和并集操作:

java 复制代码
import com.google.common.collect.Sets;

import java.util.Set;

public class SetOperationsExample {
    public static void main(String[] args) {
        Set<String> setA = ImmutableSet.of("apple", "banana", "cherry");
        Set<String> setB = ImmutableSet.of("banana", "date", "fig");

        Set<String> union = Sets.union(setA, setB);
        Set<String> intersection = Sets.intersection(setA, setB);
        Set<String> difference = Sets.difference(setA, setB);
        Set<String> symmetricDifference = Sets.symmetricDifference(setA, setB);

        System.out.println("Union: " + union);
        System.out.println("Intersection: " + intersection);
        System.out.println("Difference: " + difference);
        System.out.println("Symmetric Difference: " + symmetricDifference);
    }
}

4. 使用copyOf方法

使用copyOf方法创建不可变集合,可以避免不必要的拷贝操作:

java 复制代码
import com.google.common.collect.ImmutableSet;

import java.util.HashSet;
import java.util.Set;

public class CopyOfExample {
    public static void main(String[] args) {
        Set<String> modifiableSet = new HashSet<>();
        modifiableSet.add("apple");
        modifiableSet.add("banana");

        ImmutableSet<String> immutableSet = ImmutableSet.copyOf(modifiableSet);
        System.out.println("ImmutableSet from copyOf: " + immutableSet);
    }
}

这些示例展示了如何使用Guava库中的不可变集合以及如何进行常见的集合操作。不可变集合的优势在于它们是线程安全的,适合在多线程环境中使用,同时也能提高代码的可读性和维护性。

相关推荐
Sylvia-girl2 分钟前
Java之日志框架
java·开发语言
MengFly_9 分钟前
Java广播 —如何利用广播做服务发现
java·网络·服务发现
zqmattack11 分钟前
SQL sever根据身份证判断性别函数
java·数据库·sql
oioihoii11 分钟前
QT跨平台一次编写,处处编译
开发语言·qt
edisao12 分钟前
四。SpaceX、网络化与未来的跨越:低成本、高频次的真正威胁
大数据·开发语言·人工智能·科技·php
Macbethad12 分钟前
半导体EFEM设备TwinCAT程序设计方案
java·前端·网络
qq_3363139314 分钟前
java基础-多线程练习
java·开发语言·算法
wjs202414 分钟前
《jEasyUI 树形网格添加分页》
开发语言
我是一只小青蛙88815 分钟前
C++核心过渡:类与对象精讲
开发语言·c++
鹿角片ljp16 分钟前
Java多线程编程:从基础到实战的完整指南
java·开发语言·后端