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