Java 2's collections framework mainly includes two interfaces and their extensions and implementation classes: Collection interface and map interface. The difference between the two lies in that the former stores a group of objects, while the latter stores some key/value pairs.
Public InterfaceJava. util. Map {
// Altering methods
PublicObject put (Object key, object value );
PublicObject remove (Object key );
Public VoidPutall (Java. util. Map );
Public VoidClear ();
// Querying Methods
PublicObject get (Object key );
Public IntSize ();
Public BooleanIsempty ();
Public BooleanContainskey (object );
Public BooleanContainsvalue (object );
Public BooleanEquals (object );
// Viewing Methods
PublicJava. util. Set keyset ();// Gets keys
PublicJava. util. collection values ();// Gets values
PublicJava. util. Set entryset ();// Gets Mappings
Public Static InterfaceJava. util. Map. Entry {// A map-entry (single key/Value Pair)
PublicObject getkey ();// Returns current Entry key
PublicObject getvalue ();// Returns current entry value
PublicObject setvalue (object value );
Public BooleanEquals (object );
Public IntHashcode ();}
}
The map interface provides easy-to-use methods through which you can query, view, and modify the content of the current map. Note that a set is returned for the keyset () method of the map interface. Set is an extension of the collection interface and contains a set of non-repeated objects. Because keys in map cannot be repeated, the keyset () method of all keys returns a set object. The map interface also contains a map. Entry interface. A map. entry is a key/value pair in map. The entryset () method in the map interface returns a set object, and each element implements the map. entry interface. The get (Object Key), put (Object key, object value), and remove (Object key) Methods of the map interface have the same problem. Their return types are all objects. When null is returned, we can guess that the key does not exist before the method is called. However, this can be guessed only when null is not allowed as the MAP value. All General implementations of the map interface allow null as the key or value, which means that when a null value is returned, it can mean many things. Only because the general implementation allows null values, you cannot conclude that the ing has null values. If you are sure that there is no null value, then the return null value means that there is no key in the ing before calling that method. Otherwise, you must call containskey (Object key) to check whether the key exists.
Hashtable
Java. util. hashtable implements the map interface. In hashtable, The hashcode () of the key object is used as the relative storage address of the corresponding object, so that the object can be quickly searched based on keywords. Therefore, only one object that implements the hashcode () and equals () methods can be used as the key of hashtable. The null value cannot be used as a keyword or value.
Public ClassJava. util. hashtableExtendsDictionaryImplementsCloneable, MAP, serializable {
// Hashtable Constructors
// Construct a default hashtable with default capacity and load of 0.75
PublicHashtable ();
// Construct a hashtable with passed capacity and default load of 0.75
PublicHashtable (IntInitialcapacity );
// Construct hashtable with passed capacity and Load
PublicHashtable (IntInitialcapacity,FloatLoad );
// Construct hashtable with passed Mapping
PublicHashtable (MAP );
// Hashtable specific methods
// Checks if object is in hashtable
Public BooleanContains (object );
// Returns enumeration of elements in hashtable
PublicEnumeration elements ();
// Returns enumeration of keys in hashtable
PublicEnumeration keys ();
// Creates shallow copy of hashtable (Structure copied, but not key/values)
PublicObject clone ();
// Prints out key/value pairs of hashtable Elements
PublicString tostring ();
// Reorganizes all elements in hashtable, and increases hashtable capacity
Protected VoidRehash ();
// Get value from passed in key
PublicObject get (object );
// Insert key/value pair
PublicObject put (Object key, object value );
}
Hashtable is an old tool class before the Java 2 Collection framework was launched. It has been replaced by hashmap under the new Java 2 Collection framework. The main difference between hashtable and hashmap is that the former is synchronous, while the latter is a fast failure mechanism to ensure that no multi-thread concurrency error (fast-fail) occurs ). When initializing a hashtable, you can specify two parameters: initial capacity and load. These two parameters strongly affect the performance of hashtable. Capacity refers to the number of objects, and load refers to the ratio of the number of objects actually stored in the scattered list to the capacity. If the initial capacity is too small, hashtable needs to be constantly expanded and rehash (), which is very time-consuming. If the initial capacity is too large, it will cause a waste of space. On the contrary, if the load is too small, it will cause a waste of space. If the load is too large, it will take time (because this will lead to repeated hash codes with more keywords, hashtable uses a chain table to store objects with repeated hash codes ). The default value of capacity is 11, and the default value of load is 0.75. Generally, you can use the default value to generate a hashtable. In addition, most methods in hashtable are synchronized.
Hashmap
Hashmap basically implements all the methods of the map interface. For details about the method signature, refer to the above map interface. Here are some methods in the map interface.
According to the implementation of the set framework, the hash table is a single-chain table as an array of elements. Two or more entries with the same index value are linked together to the single-chain table. The hash table declaration is as follows:
Private entry [] table;
The component type entry is the implementation of the map. Entry interface, and the map. Entry is declared in the map interface. The following is a simplified implementation of the map. Entry interface:
Private Static ClassEntryImplementsMap. Entry {
IntHashcode;
Object key;
Object value;
Entry next;
Entry (IntHashcode, object key, object value, entry next ){
This. hashcode = hashcode;
This. Key = key;
This. value = value;
This. Next = next;
}
PublicObject getkey (){
ReturnKey;
}
PublicObject getvalue (){
ReturnValue;
}
PublicObject setvalue (object Value ){
Object oldvalue =This. Value;
This. value = value;
Return oldvalue;
}
}
Sortedmap is a sub-interface of the map interface. The standard implementation of sortedmap is treemap, which implements a sorted map. I will not explain it here. Java rules translated on this site have an in-depth study on the Collection framework of Java2. This article is limited to space and only fur. If you are interested, please refer to the collection implementation code and API doc in Java2 source code.