Java Collections unmodifiableNavigableMap() 方法及示例
Java集合中的unmodifiableMap()用于获取给定地图的不可修改视图。
句法:
public static Map unmodifiableMap(Map extends Key, ? extends Key> map)
参数:
- Key是键值类型
- 值是值类型
- 地图是输入地图
返回:它将返回给定地图的不可修改视图。
异常:此方法不会抛出任何异常。
示例 1:
Java
// Java program to create a map with
// string values and get the
// unmodifiable view
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// create hashmap
HashMap data
= new HashMap();
// add elements to the created hashmap
data.put("1", "manoj");
data.put("2", "sai");
data.put("3", "ramya");
data.put("4", "sravan");
// display the data
System.out.println(data);
// Create unmodifiable map for the above map
Map final1
= Collections.unmodifiableMap(data);
// display unmodifiable map
System.out.println(final1);
}
}
Java
// Java program to create a map
// with integer values and get
// the unmodifiable view
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// create hashmap
HashMap data
= new HashMap();
// add elements to the created hashmap
data.put(1, 67);
data.put(2, 34);
// display the data
System.out.println(data);
// Create unmodifiable map for the above map
Map final1
= Collections.unmodifiableMap(data);
// display unmodifiable map
System.out.println(final1);
}
}
Java
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// create hashmap
HashMap data
= new HashMap();
// add elements to the created hashmap
data.put(1, 67);
data.put(2, 34);
// display the data
System.out.println(data);
// Create unmodifiable map for the above map
Map final1
= Collections.unmodifiableMap(data);
// put value in unmodifiable map
final1.put(3, 45);
// display unmodifiable map
System.out.println(final1);
}
}
输出
{1=manoj, 2=sai, 3=ramya, 4=sravan}
{1=manoj, 2=sai, 3=ramya, 4=sravan}
示例 2:
Java
// Java program to create a map
// with integer values and get
// the unmodifiable view
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// create hashmap
HashMap data
= new HashMap();
// add elements to the created hashmap
data.put(1, 67);
data.put(2, 34);
// display the data
System.out.println(data);
// Create unmodifiable map for the above map
Map final1
= Collections.unmodifiableMap(data);
// display unmodifiable map
System.out.println(final1);
}
}
输出
{1=67, 2=34}
{1=67, 2=34}
示例 3:当我们将元素添加到可修改的地图时,它会抛出错误。
Java
import java.util.*;
public class GFG {
// main method
public static void main(String[] args)
{
// create hashmap
HashMap data
= new HashMap();
// add elements to the created hashmap
data.put(1, 67);
data.put(2, 34);
// display the data
System.out.println(data);
// Create unmodifiable map for the above map
Map final1
= Collections.unmodifiableMap(data);
// put value in unmodifiable map
final1.put(3, 45);
// display unmodifiable map
System.out.println(final1);
}
}
输出:
{1=67, 2=34}
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableMap.put(Collections.java:1459)
at GFG.main(GFG.java:21)