java.util.Collections類的checkedSortedMap()方法用於返回指定排序映射的動態類型安全視圖。
如果指定的映射是可序列化的,則返回的映射將是可序列化的。
由於null被認為是任何引用類型的值,因此,隻要有支持映射,返回的映射就允許插入null鍵或值。
用法:
public static SortedMap checkedSortedMap(SortedMap m, Class keyType, Class valueType)
參數:此方法將以下參數作為參數
- m –要為其返回動態類型安全視圖的Map
- keyType –m允許持有的 key 的類型
- valueType –m允許持有的值的類型
返回值:此方法返回指定映射的動態類型安全視圖。
以下示例說明了checkedSortedMap()方法
示例1:
// Java program to demonstrate
// checkedSortedMap() method
// for <String, String> type
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// creating object of SortedMap<String, String>
SortedMap<String, String>
smap = new TreeMap<String, String>();
// Adding elemnet to smap
smap.put("Ram", "Gopal");
smap.put("Karan", "Arjun");
smap.put("Karn", "Veer");
// printing the sorted map
System.out.println("Sorted map:\n"
+ smap);
// create typesafe view of the specified map
SortedMap<String, String>
tsmap = Collections
.checkedSortedMap(smap,
<strong>Output:</strong>
<pre>{Karan=39, Karn=40, Ram=20}</pre>
String.class,
String.class);
// printing the typesafe view of specified sorted map
System.out.println("Typesafe view of sorted map:\n"
+ tsmap);
}
catch (IllegalArgumentException e) {
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
Sorted map: {Karan=Arjun, Karn=Veer, Ram=Gopal} Typesafe view of sorted map: {Karan=Arjun, Karn=Veer, Ram=Gopal}
示例2:
// Java program to demonstrate
// checkedSortedMap() method
// for <String, Integer> type
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating object of SortedMap<String, Integer>
SortedMap<String, Integer> smap = new TreeMap<String, Integer>();
// Adding elemnet to smap
smap.put("Ram", 20);
smap.put("Karan", 39);
smap.put("Karn", 40);
// printing the sorted map
System.out.println("Sorted map:\n"
+ smap);
// create typesafe view of the specified map
SortedMap<String, Integer>
tsmap = Collections
.checkedSortedMap(smap,
String.class,
Integer.class);
// printing the typesafe view of specified sorted map
System.out.println("Typesafe view of sorted map:\n"
+ tsmap);
}
catch (IllegalArgumentException e) {
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
Sorted map: {Karan=39, Karn=40, Ram=20} Typesafe view of sorted map: {Karan=39, Karn=40, Ram=20}
相關用法
- Java Collections max()用法及代碼示例
- Java Collections min()用法及代碼示例
- Java Collections asLifoQueue()用法及代碼示例
- Java Collections synchronizedList()用法及代碼示例
- Java Collections synchronizedCollection()用法及代碼示例
- Java Collections swap()用法及代碼示例
- Java Collections unmodifiableCollection()用法及代碼示例
- Java Collections list()用法及代碼示例
- Java Collections newSetFromMap()用法及代碼示例
- Java Collections replaceAll()用法及代碼示例
- Java Collections synchronizedMap()用法及代碼示例
- Java Collections singletonMap()用法及代碼示例
- Java Collections singletonList()用法及代碼示例
- Java Collections checkedSet()用法及代碼示例
- Java Collections synchronizedSet()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Collections checkedSortedMap() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。