- ImmutableList,顾名思义,是一种不可变的列表。这意味着列表的内容在声明之后是固定的或不变的,也就是说,它们是固定的 只读 .
- 如果试图添加、删除和更新列表中的元素, 不支持操作异常 被扔了。
- ImmutableList也不允许空元素。
- 如果有人试图用null元素创建不可变列表, 空指针异常 被扔了。如果试图在列表中添加空元素, 不支持操作异常 被扔了。
不可变列表的优点
null
- 他们是 线程安全。
- 他们是 内存效率高。
- 因为它们是不可变的,所以它们可以 转交给第三方图书馆 没问题。
笔记 它是一个不可变的集合,而不是不可变对象的集合,因此可以修改其中的对象。
班级声明:
@GwtCompatible(serializable=true, emulated=true) public abstract class ImmutableList extends ImmutableCollection implements List, RandomAccess
类层次结构:
java.lang.Object ↳ java.util.AbstractCollection ↳ com.google.common.collect.ImmutableCollection ↳ com.google.common.collect.ImmutableList
创建不可变列表 ImmutableList可以通过多种方法创建。这些措施包括:
- 使用Guava的copyOf()函数从现有列表中删除
// Below is the Java program to create ImmutableList
import
com.google.common.collect.ImmutableList;
import
java.util.*;
class
GFG {
// Function to create ImmutableList from List
public
static
<T>
void
iList(List<T> list)
{
// Create ImmutableMap from Map using copyOf()
ImmutableList<T> immutableList =
ImmutableList.copyOf(list);
// Print the ImmutableMap
System.out.println(immutableList);
}
public
static
void
main(String[] args)
{
List<String> list =
new
ArrayList<>(
Arrays.asList(
"Geeks"
,
"For"
,
"Geeks"
));
iList(list);
}
}
输出:
[Geeks, For, Geeks]
- 使用Guava()函数的新ImmutableList
// Below is the Java program to create ImmutableList
import
com.google.common.collect.ImmutableList;
import
java.util.*;
class
GFG {
// Function to create ImmutableList
public
static
void
iList()
{
// Create ImmutableList using of()
ImmutableList<String> immutableList =
ImmutableList.of(
"Geeks"
,
"For"
,
"Geeks"
);
// Print the ImmutableMap
System.out.println(immutableList);
}
public
static
void
main(String[] args)
{
iList();
}
}
输出:
[Geeks, For, Geeks]
- 使用Java 9 Factory Of()方法
在Java中,将()与Set、Map或List一起使用,以创建一个不可变的列表。
请注意:下面的程序是Java 9的。因此,您需要一个Java 9编译器来运行它们。
// Java code illustrating of() method to
// create a ImmutableSet
import
java.util.*;
import
com.google.common.collect.ImmutableList;
class
GfG {
public
static
void
main(String args[])
{
// non-empty immutable set
List<String> list = List.of(
"Geeks"
,
"For"
,
"Geeks"
);
// Let's print the list
System.out.println(list);
}
}
输出:
[Geeks, For, Geeks]
- 使用 建筑商() 来自不可变列表
在Guava中,ImmnutableList类提供了一个函数生成器()。通过这个函数,可以创建一个新的不可变列表,或者 可以从现有列表创建不可变列表,也可以同时从现有列表和/或现有列表创建不可变列表。
- 创建新的不可变列表
// Java code illustrating of() method to
// create a ImmutableList
import
java.util.*;
import
com.google.common.collect.ImmutableList;
class
GfG {
public
static
void
main(String args[])
{
// non-empty immutable set
ImmutableList<String> iList = ImmutableList.<String>builder()
.add(
"Geeks"
,
"For"
,
"Geeks"
)
.build();
// Let's print the List
System.out.println(iList);
}
}
输出:
[Geeks, For, Geeks]
- 从现有列表创建不可变列表
// Java code illustrating of() method to
// create a ImmutableList
import
java.util.*;
import
com.google.common.collect.ImmutableList;
class
GfG {
public
static
void
main(String args[])
{
// non-empty immutable set
List<String> list = List.of(
"Geeks"
,
"For"
,
"Geeks"
);
ImmutableList<String> iList = ImmutableList.<String>builder()
.addAll(list)
.build();
// Let's print the List
System.out.println(iList);
}
}
输出:
[Geeks, For, Geeks]
- 创建包含现有列表的新不可变列表
// Java code illustrating of() method to
// create a ImmutableList
import
java.util.*;
import
com.google.common.collect.ImmutableList;
class
GfG {
public
static
void
main(String args[])
{
// non-empty immutable set
List<String> list = List.of(
"Geeks"
,
"For"
,
"Geeks"
);
ImmutableList<String> iList = ImmutableList.<String>builder()
.addAll(list)
.add(
"Computer"
,
"Portal"
, )
.build();
// Let's print the set
System.out.println(iList);
}
}
输出:
[Geeks, For, Geeks, Computer, Portal]
- 创建新的不可变列表
尝试更改不可变列表
如前所述,下面的程序将抛出 不支持操作异常 .
// Java code to show that UnsupportedOperationException // will be thrown when ImmutableList is modified. import java.util.*; class GfG { public static void main(String args[]) { // empty immutable map List<String> list = List.of(); // Lets try adding element in List List.add( "Geeks" ); } } |
输出:
Exception in thread "main" java.lang.UnsupportedOperationException at com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:218) at ImmutableListDemo.main(Main.java:16)
它与收藏品有何不同。不可修改列表()?
收藏。unmodifiableList会围绕同一个现有列表创建一个包装器,这样包装器就不能用来修改它。不过,我们仍然可以更改原始列表。
// Java program to demonstrate that a List created using // Collections.unmodifiableList() can be modified indirectly. import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add( "Geeks" ); // Create ImmutableList from List using copyOf() List<String> iList = Collections.unmodifiableList(list); // We change List and the changes reflect in iList. list.add( "For" ); list.add( "Geeks" ); System.out.println(iList); } } |
输出:
[Geeks, For, Geeks]
如果我们从现有列表创建不可变列表并更改现有列表,则不可变列表不会因为创建了副本而更改。
// Below is a Java program for // Creating an immutable List using copyOf() // and modifying original List. import java.io.*; import java.util.*; import com.google.common.collect.ImmutableList; class GFG { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add( "Geeks" ); // Create ImmutableList from List using copyOf() ImmutableList<String> iList = ImmutableList.copyOf(list); // We change List and the changes wont reflect in iList. list.add( "For" ); list.add( "Geeks" ); System.out.println(iList); } } |
输出:
[Geeks]
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END