Java Reference
In-Depth Information
Although the capacity will automatically increase when necessary, your program will
run more efficiently if the capacity is initially set to the number of elements you expect
will be added to the map.
The
HashMap<K,V>
, of course, implements all the methods in the
Map<K,V>
inter-
face and adds no other methods beyond constructors and an implementation of the
clone( )
method. A summary of the
HashMap<K,V>
constructors and
clone( )
is
given in Display 16.10.
Display 16.10
Methods in the
HashMap<K,V>
Class
The
HashMap<K,V>
class is in the
java.util
package.
The
HashMap<K,V>
class extends the
AbstractMap<K,V>
class and implements the
Map<K,V>
interface.
The
HashMap<K,V>
class implements all of the methods in the
Map<K,V>
interface (Display 16.9).
The only other methods in the
HashMap<K,V>
class are the constructors.
All the exception classes mentioned are the kind that are not required to be caught in a
catch
block or declared in a
throws
clause.
All the exception classes mentioned are in the package
java.lang
and so do not require any
import statement.
public
HashMap( )
Creates a new, empty map with a default initial capacity of 16 and load factor of 0.75.
public
HashMap(
int
initialCapacity)
Creates a new, empty map with a default capacity of
initialCapacity
and load factor of 0.75.
Throws a
IllegalArgumentException
if
initialCapacity
is negative.
public
HashMap(
int
initialCapacity, float loadFactor)
Creates a new, empty map with the specified capacity and load factor.
Throws a
IllegalArgumentException
if
initialCapacity
is negative or
loadFactor
nonpos-
itive.
public
HashMap(Map<?
extends
K,?
extends
V> m)
Creates a new map with the same mappings as
m
. The
initialCapacity
is set to the same size
as
m
and the
loadFactor
to 0.75.
Throws a
NullPointerException
if
m
is
null
.
public
Object clone( )
Creates a shallow copy of this instance and returns it. The keys and values are not cloned.
The remainder of the methods are the same as those described for the
Map<K,V>
interface
(Display 16.9.)