public
TreeMap(SortedMap<K, ? extends V> m)
Constructs
a new tree map containing the same mappings and using the same
ordering as the specified sorted map.
import java.util.*; class TreeMapConstructor4{ public static void main(String args[]){ TreeMap<Integer, String> myMap1; TreeMap<Integer, String> myMap2; SortedMap<Integer, String> subMap; myMap2 = new TreeMap<> (); /* Add Elements to myMap2 */ myMap2.put(10, "c"); myMap2.put(1, "a"); myMap2.put(15, "d"); myMap2.put(5, "b"); myMap2.put(99, "e"); subMap = myMap2.subMap(5, 20); System.out.println("Elements in myMap2 are"); System.out.println(myMap2); System.out.println("Elements in subMap are"); System.out.println(subMap); myMap1 = new TreeMap<> (subMap); System.out.println("Elements in myMap1 are"); System.out.println(myMap1); } }
Output
Elements in myMap2 are {1=a, 5=b, 10=c, 15=d, 99=e} Elements in subMap are {5=b, 10=c, 15=d} Elements in myMap1 are {5=b, 10=c, 15=d}
1. Throws
NullPointerException if the specified map is null
import java.util.*; class TreeMapConstructor4NullPointer{ public static void main(String args[]){ TreeMap<Integer, String> myMap1; SortedMap<Integer, String> subMap; subMap = null; myMap1 = new TreeMap<> (subMap); System.out.println("Elements in myMap1 are"); System.out.println(myMap1); } }
Output
Exception in thread "main" java.lang.NullPointerException at java.util.TreeMap.<init>(TreeMap.java:198) at TreeMapConstructor4NullPointer.main(TreeMapConstructor4NullPointer.ja va:10)
No comments:
Post a Comment