Transcript Slide 1

Hashing in java.util
http://java.sun.com/j2se/1.5.0/docs/api/index.html
HashMap
• A map is a collection that holds pairs
(key,value), or entries.
• HashMap is an implementation of the
interface Map (read main section 5.5,
p.275)
• Class hierarchy in java.util is as follows:
Object
AbstractMap
HashMap
2
HashMap
• Uses chaining as collision resolution technique
• HashMap permits null values and the null key.
• HashMap implementation is not synchronized,
i.e. If multiple threads access this map
concurrently, and at least one of the threads
modifies the map structurally, then other
threads may have unsynchronized access to
the map.
3
HashMap
• An instance of HashMap has two
parameters that affect its performance:
initial capacity and load factor (default
value =0.75).
• When the number of entries in the hash
table exceeds the product of the load
factor and the current capacity, the
capacity is roughly doubled by calling a
rehash method.
4
HashMap
• Methods in class HashMap include:
– Constructor methods:
HashMap()
Constructs an empty HashMap with the default
initial capacity (16) and the default load factor (0.75).
HashMap(int ic)
Constructs an empty HashMap with the specified
initial capacity and the default load factor (0.75).
5
HashMap
– Constructor methods (continue):
HashMap(int ic, float lf)
Constructs an empty HashMap with the specified
initial capacity and load factor.
HashMap(Map m)
Constructs a new HashMap with the same elements
from the specified Map m.
6
HashMap Methods
void clear()
Removes all mappings from this hash map.
boolean containsKey(Object key)
Returns true if this map contains a mapping for the
specified key.
boolean containsValue(Object value)
Returns true if this map maps one or more keys to
the specified value.
7
HashMap Methods (continue)
Set entrySet()
Returns a set containing all the pairs (key, value) in
the hash map.
Object get(Object key)
Returns the object associated with key.
boolean isEmpty()
Return true if this map contains no elements, false
otherwise.
8
HashMap Methods (continue)
Object put(Object key, Object val)
Put the pair(key,val) in the hash map; return a value
associated with key if there is any in the hash map,
null otherwise.
void putAll(Map m)
Add objects from map m to the current hash map.
Void rehash()
A protected method to increase the capacity of the
hashtable; the method is called automatically when the
number of keys in the hashtable is greater than the
product of the load factor and the current capacity.
9
HashMap Methods (continue)
Object remove(Object key)
Remove the pair (key, corresponding value) from the
hash map and return the value associated currently
with key in the hash map.
int size()
Returns the number of objects in this map
10
HashSet
• A set is an object that stores unique
elements.
• HashSet is an implementation of the
interface Set
• Class hierarchy in java.util is as follows:
Object
AbstractCollection
AbstractSet
HashSet
11
HashSet
• Methods in class HashSet include:
– Constructor methods:
HashSet()
Creates an empty HashSet with initial capacity (16)
and load factor (0.75).
HashSet(int ic)
Constructs an empty HashSet with the specified
initial capacity and the default load factor (0.75).
12
HashSet
– Constructor methods (continue):
HashSet(int ic, float lf)
Constructs an empty HashSet with the specified
initial capacity and load factor.
HashSet(Collection c)
Constructs a new HashSet with the same elements
from the specified Collection c.
13
HashSet Methods
boolean add(Object ob)
Adds the specified element to this set if it is not
already present
void clear()
Remove all the objects from the hash set
boolean contains(Object ob)
Return true if the hash set contains the object ob
boolean isEmpty()
Returns true if this set contains no elements.
14
HashSet Methods (continue)
boolean remove(Object ob)
Removes the specified element from this set if it is
present.
int size()
Returns the number of elements in this set (its
cardinality).
15
Hashtable
• This is another implementation of the
interface Map.
• It is roughly equivalent to HashMap except
that it is synchronized and does not permit
null keys or null values.
• Class hierarchy in java.util is as follows:
Object
Dictionary
Hashtable
• Check: http://java.sun.com/j2se/1.5.0/docs/api/index.html
16