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库中的不可变集合以及如何进行常见的集合操作。不可变集合的优势在于它们是线程安全的,适合在多线程环境中使用,同时也能提高代码的可读性和维护性。