在Java使用 Guava 的收集器收集流到不可变集合
Guava 是一组来自 Google 的核心库,其中包括新的集合类型。例如,我们多映射和多集、不可变集合、图形库和用于并发的实用程序。它被许多公司使用。在本文中,我们将看到如何使用 Guava 的收集器在Java中将流收集到不可变集合。要使用番石榴,我们需要添加 Maven 依赖项。否则,我们必须将这个库从外部添加到我们的项目中。
插图:
com.google.guava
guava
22.0
为了清楚起见,让我们在跳转到实现之前先了解涉及之前涉及的方法的相同重要概念。流的概念在这里扮演着重要的角色,它是在Java 8 中引入的。流表示一系列元素并支持不同类型的操作来对这些元素执行计算或批量操作。
- 流不是数据结构,而是从集合、数组或 I/O 通道获取输入。
- Streams 不会改变原始数据结构,它们仅根据流水线方法提供结果。
- 每个中间操作都被延迟执行并作为结果返回一个流,因此可以将各种中间操作流水线化。终端操作标记流的结束并返回结果。
句法:
static IntStream range (int start, int end)
返回类型:整数流
描述:该方法用于返回整数流的顺序
Stream intStream boxed()
返回类型:流
描述:此方法返回由此流的元素组成的流
示例 1:
Java
// Java Program to illustrate Use Guava's Collectors
// for Collecting Streams to Immutable Collections
// Importing utility classes from java.util package
// Importing guava Library
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
// Main class
public class App {
// Main driver method
public static void main(String[] args)
{
// Creating a stream of Integers from 0 to 9
// by using boxed method coverting that stream
// into stream of elements
List list
= IntStream.range(0, 9).boxed().collect(
ImmutableList.toImmutableList());
// printing the list
System.out.println(list);
}
}
Java
// Java Program to illustrate Use Guava's Collectors
// for Collecting Streams to Immutable Collections
// Where UnsupportedOperationException is thrown
// Importing utility classes from java.util package
// Importing Guava library
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
// Main class
public class App {
// Main driver method
public static void main(String[] args)
{
// Creating a stream of Integers from 0 to 9
// by using boxed method coverting that stream
// into stream of elements
List list
= IntStream.range(0, 9).boxed().collect(
ImmutableList.toImmutableList());
// Adding more elements to immutatble list
// Note: Trying to add
list.add(12);
list.add(13);
list.add(14);
// Print and display the List
System.out.println(list);
}
}
输出:
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Note: In the above output we have printed an immutable list of Integers. If we try to modify the list, it will throw an UnsupportedOperationException.
示例 2:
Java
// Java Program to illustrate Use Guava's Collectors
// for Collecting Streams to Immutable Collections
// Where UnsupportedOperationException is thrown
// Importing utility classes from java.util package
// Importing Guava library
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
// Main class
public class App {
// Main driver method
public static void main(String[] args)
{
// Creating a stream of Integers from 0 to 9
// by using boxed method coverting that stream
// into stream of elements
List list
= IntStream.range(0, 9).boxed().collect(
ImmutableList.toImmutableList());
// Adding more elements to immutatble list
// Note: Trying to add
list.add(12);
list.add(13);
list.add(14);
// Print and display the List
System.out.println(list);
}
}
输出:
Exception in thread "main" java.lang.UnsupportedOperationException
at com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:220)
at james.App.main(App.java:15)