




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















docsity.com
import java.util.*; Hashtable table = new Hashtable();
docsity.com
Hashtable()
Constructs a new, empty Hashtable with a default capacity ( 11 ) and default load factor (0.75).
Constructs a new, empty Hashtable with the specified initial capacity and the default load factor (0.75).
Constructs a new, empty Hashtable with the specified initial capacity and the specified load factor.
Constructs a new Hashtable with the same mappings as the given Map.
docsity.com
A hash table that is mostly empty wastes space If a hash table is nearly full, some searches may take a very long time
A load factor of 75% is usually a good compromise If the table gets fuller than the load factor, Java creates a new, larger hash table and rehashes everything Rehashing is an expensive operation docsity.com
Collection
SortedSet
Set List SortedMap
Map
Hashtable
docsity.com
V put(K key , V value ) Returns the previous value associated with key, or null if there was no previous value V get(Object key ) Returns null if the key was not found A return value of null may not mean the key was not found (some implementations of Map allow null keys and values)
boolean containsKey(Object key ) boolean containsValue(Object value ) Warning: probably requires linear time! boolean isEmpty() boolean equals(Object o ) Returns true if o is also a map and has the same mappings docsity.com
public void remove(Object key) throws UnsupportedOperationException { throw new UnsupportedOperationException(); }
docsity.com
Set
Returns a set view of the keys contained in this map.
Collection
Set<Map.Entry<K, V>> entrySet() Returns a set view of the mappings contained in this map.
A view is dynamic access into the Map If you change the Map, the view changes If you change the view, the Map changes
The Map interface does not provide any Iterators However, there are iterators for the above Sets and Collections
docsity.com
A no-argument constructor for each Map type A constructor that takes a Map argument, and copies its key- value pairs into the new Map
Defining your own Map class is easy: class MyMap implements Map { ... } There are, however, a lot of methods to implement
docsity.com
In order for a Hashtable to work correctly,
equals must be defined properly on the keys hashCode must be defined properly on the keys
This is not a problem if you use Strings for the keys (this is extremely common)
If you use objects of some other class as your keys, you must make sure equals and hashCode are properly defined
Note: equals and hashCode are properly defined for all of Java’s Maps; it’s the keys that you need to be careful with
docsity.com
Actually, except for the constructors, I’ve been talking about the Map interface, which both Hashtable and HashMap implement Both are cloneable (more on this later) and serializable
Hashtable is synchronized; HashMap is not HashMap permits null values and (one) null key; Hashtable does not
docsity.com
synchronized
A Thread is an execution sequence Having multiple Threads means that Java appears to be doing many different things all at the same time Threads can interfere with each other unless they are carefully synchronized (prevented from both using the same data at the same time) This can be an issue with GUIs, which run in a different Thread from the rest of the program If you use a hash table from an event handler, use a Hashtable (which is synchronized) instead of a HashMap (which is not)
docsity.com
Cloneable, like Serializable, is a marker interface: it doesn't require any methods It does, however, allow you to use the clone method class Person implements Cloneable { ... } ... Person jack = john.clone(); clone() makes a shallow copy
If you want a deep copy, you have to write a lot more code
Avoid making copies if possible; it’s not easy and it’s expensive
"John" 23
john
"John" 21
"Mary"
jack
"John" 23
docsity.com
Rather than use cloneable, it’s usually better to write a copy constructor—a constructor that takes an object as a parameter and makes another object just like it
Example: Person jack = new Person(john);
There is nothing magic about a copy constructor—it’s up to you to make a deep copy rather than a shallow copy
Person (Person original) { this.name = original.name; this.spouse = new Person(original.spouse); this.spouse.spouse = this; // why? }
Does this actually work?
docsity.com