HashMap(int
initialCapacity, float loadFactor)
Constructs
an empty HashMap with the specified initial capacity and load factor.
import java.util.*; class HashMapConstructor3{ public static void main(String args[]){ Map<Integer, String> myMap = new HashMap<> (10, 0.9f); /* Adding Data to myMap */ myMap.put(1, "First"); myMap.put(2, "Second"); myMap.put(3, "Third"); myMap.put(4, "Fourth"); System.out.println("Elements in myMap are"); System.out.println(myMap); } }
Output
Elements in myMap are {1=First, 2=Second, 3=Third, 4=Fourth}
1.
throws IllegalArgumentException if the initial capacity is negative
or
the load factor is nonpositive
import java.util.*; class HashMapConstructor3Illegal{ public static void main(String args[]){ Map<Integer, String> myMap = new HashMap<> (10, 0); } }
Output
Exception in thread "main" java.lang.IllegalArgumentException: Illegal load fact or: 0.0 at java.util.HashMap.<init>(HashMap.java:273) at HashMapConstructor3Illegal.main(HashMapConstructor3Illegal.java:5)
No comments:
Post a Comment