- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Collections checkedSortedMap() Method
Description
The checkedSortedMap(SortedMap<, V>, Class<K>, Class<V>) method is used to get a dynamically typesafe view of the specified map.
Declaration
Following is the declaration for java.util.Collections.checkedSortedMap() method.
public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K,V> m,Class<K> keyType,Class<V> valueType)
Parameters
m − This is the sorted map for which a dynamically typesafe view is to be returned.
keyType − This is the type of key that m is permitted to hold.
valueType − This is the type of value that m is permitted to hold.
Return Value
The method call returns a dynamically typesafe view of the specified sorted map.
Exception
NA
Getting TypeSafe SortedMap from a SortedMap of String, Integer Pair Example
The following example shows the usage of Java Collection checkedSortedMap(SortedMap,Class,Class ) method to get a typesafe view of map of String and Integers. We've created a Map object with some string and integers. Using checkedSortedMap(SortedMap, String, Integer) method, we're getting a map of String, Integer and then it is printed.
package com.tutorialspoint; import java.util.Collections; import java.util.SortedMap; import java.util.TreeMap; public class CollectionsDemo { public static void main(String args[]) { // create map SortedMap<String,Integer> hmap = new TreeMap<>(); // populate the map hmap.put("1", 1); hmap.put("2", 2); hmap.put("3", 3); hmap.put("4", 4); // get typesafe view of the map SortedMap<String,Integer> tsmap; tsmap = Collections.checkedSortedMap(hmap,String.class,Integer.class); System.out.println("Dynamically typesafe view of the map: "+tsmap); } }
Output
Let us compile and run the above program, this will produce the following result.
Dynamically typesafe view of the map: {1=1, 2=2, 3=3, 4=4}
Getting TypeSafe SortedMap from a SortedMap of String, String Pair Example
The following example shows the usage of Java Collection checkedSortedMap(SortedMap,Class,Class ) method to get a typesafe view of map of String and Strings. We've created a Map object with some string and strings. Using checkedSortedMap(SortedMap, String, String) method, we're getting a map of String, String and then it is printed.
package com.tutorialspoint; import java.util.Collections; import java.util.SortedMap; import java.util.TreeMap; public class CollectionsDemo { public static void main(String args[]) { // create map SortedMap<String,String> hmap = new TreeMap<>(); // populate the map hmap.put("1", "A"); hmap.put("2", "B"); hmap.put("3", "C"); hmap.put("4", "D"); // get typesafe view of the map SortedMap<String,String> tsmap; tsmap = Collections.checkedSortedMap(hmap,String.class,String.class); System.out.println("Dynamically typesafe view of the map: "+tsmap); } }
Output
Let us compile and run the above program, this will produce the following result.
Dynamically typesafe view of the map: {1=A, 2=B, 3=C, 4=D}
Getting TypeSafe SortedMap from a SortedMap of String, Object Pair Example
The following example shows the usage of Java Collection checkedSortedMap(SortedMap,Class ) method to get a typesafe view of map of Student objects. We've created a map with some student objects, printed the original map. Using checkedSortedMap(SortedMap, Student) method, we're getting a sorted map of String and then it is printed.
package com.tutorialspoint; import java.util.Collections; import java.util.SortedMap; import java.util.TreeMap; public class CollectionsDemo { public static void main(String[] args) { // create map SortedMap<String,Student> hmap = new TreeMap<>(); // populate the map hmap.put("1", new Student(1, "Julie")); hmap.put("2", new Student(2, "Robert")); hmap.put("3", new Student(3, "Adam")); hmap.put("4", new Student(4, "Jene")); // get typesafe view of the map SortedMap<String,Student> tsmap; tsmap = Collections.checkedSortedMap(hmap,String.class,Student.class); System.out.println("Dynamically typesafe view of the map: "+tsmap); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }
Output
Let us compile and run the above program, this will produce the following result.
Dynamically typesafe view of the map: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ], 4=[ 4, Jene ]}
To Continue Learning Please Login
Login with Google