Java Reference
In-Depth Information
The
HashMap<K,V>
, of course, implements all the methods in the
Map<K,V>
interface 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 .
As with the
HashSet<T>
class, if you intend to use your own class as the parameterized
type
K
in a
HashMap<K,V>
then your class must override the following methods:
public int
hashCode();
public boolean
equals(Object obj);
These methods are required for indexing and checking for uniqueness of the key.
See the discussion in Section 16.1 about overriding these methods and Section 15.5
about hash functions.
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 an
IllegalArgumentException
if
initialCapacity
is negative.
public
HashMap(
int
initialCapacity, float loadFactor)
Creates a new, empty map with the specified capacity and load factor.
Throws an
IllegalArgumentException
if
initialCapacity
is negative or
loadFactor
is
nonpositive.
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).