HashSet HashTable and TreeSet, hashsettreeset
HashSet <T> class
The HashSet <T> class is mainly designed for performing high-performance set operations, such as intersection, union, and difference sets for two sets. Set contains a group of elements that do not repeat and have no feature order.
Some features of HashSet <T> are as follows:
1. The values in HashSet <T> cannot be repeated and there is no sequence.
2. The capacity of the HashSet <T> is automatically added as needed.
Constructor:
By default, HashSet () equals the comparator to create an empty new instance.
HashSet (IEnumerable <T> collection) Copies data from the collection in the specified set to the set
HashSet (IEqualityComparer <T> comparer) creates an empty new instance using the specified equality Comparator
HashSet (IEnumerable <T> collection, IEqualityComparer <T> comparer) instantiates data using the specified comparator and copies elements in the specified set to the set.
Because HashSet <T> is specially designed for set operations, many of the methods it provides are related to set operations.
The following describes some of its common methods.
Member type description
The Add method adds the specified element to the set.
The Clear method clears all elements in the set.
The Contains method determines whether an element is in the HashSet <T>
Exists method to determine whether HashSet <T> contains elements matching the specified condition
The ExceptWith method removes all elements in the specified set from the current HashSet <T>.
The IntersectWith method modifies the current HashSet <T> object to only include the object and elements in the specified set.
IsProperSubsetOf method to determine whether the HashSet <T> object is a real subset of the specified set
IsProperSupersetOf method to determine whether the HashSet <T> object is a real superset of the specified set
IsSunsetOf method to determine whether the HashSet <T> object is a subset of the specified set
IsSupersetOf method to determine whether the HashSet <T> object is a superset of the specified set
The Remove method removes the specified element from the HashSet <T> object.
The RemoveWhere method removes all elements that match the conditions defined by the specified predicate from the HashSet <T> set.
The SetEquals method determines whether the HashSet <T> object and the specified set contain the same element.
The syncemricexceptwith method modifies the current HashSet <T> object to only include this object or elements in the specified set.
The TrimExcess method sets the capacity of the HashSet <T> object to the actual number of elements it contains, and rounds up to a close feature and implementation value.
The UnionWith method modifies the current HashSet <T> object to include the object and all elements in the specified set.
TreeSet
TreeSet is implemented by TreeMap.
TreeSet is an ordered set. The elements in the TreeSet are arranged in ascending order. By default, they are arranged in natural order. This means that the elements in the TreeSet must implement the Comparable interface. Or there is a custom comparator.
When constructing a TreeSet object, we can pass the Comparator object that implements the Comparator interface.
Differences between TreeSet and HashSet
1. HashSet is implemented through HashMap, and TreeSet is implemented through TreeMap, except that Set only uses the Map key.
2. The key and Set of Map have a common feature that is the uniqueness of the Set. TreeMap has an additional sorting function.
3. hashCode and equal () are used by HashMap. Because no sorting is required, you only need to pay attention to location and uniqueness.
A. hashCode is used to calculate the hash value, and the hash value is used to determine the index of the hash table.
B. An index in the hash table stores a linked list. Therefore, you must use the equal method to cycle the objects in the comparison chain to locate the Entry corresponding to the key value.
C. put: If the hash table is not located, add an Entry in front of the linked list. If it is located, replace the value in the Entry and return the old value.
4. Because TreeMap needs to be sorted, a Comparator is required to compare the size of the key value. Of course, it is also located using Comparator.
A. Comparator can specify
B. If it is not determined during creation, the key. compareTo () method will be used, which requires that the key must implement the Comparable interface.
C. TreeMap is implemented using the Tree data structure, so you can use the compare interface to complete the positioning.
HashTable
Hashtables is no longer a new concept in the computer field. They are used to accelerate the processing speed of computers. They are processed using today's standards, which is very slow, and they allow you to query many data entries, quickly find a special entry. Although the speed of modern machines is several thousand times faster, hashtables is still a useful method to get the best performance of applications.
The Hashtable and HashMap objects allow you to combine a key with a value and usePut() Method to input the key/value pair to the table. Then you can call the get () method and take the key as the parameter to get the value ). As long as two basic requirements are met, the key and value can be any object. Note that because key and value must be objects, the original type (primitive types) must be converted to an object by using methods such as Integer (int.
To use a specific class object as a key, this class must provide two methods: equals () and hashCode (). The two methods areJava. Lang. object, so all classes can inherit these two methods. However, the implementation of these two methods in the Object class is generally useless, so you usually need to reload these two methods by yourself.
The Equals () method compares its object with another object. If the two objects represent the same information, true is returned. This method also checks and ensures that the two objects belong to the same class. If the two reference objects are exactly the same, Object. equals () returns true, which explains why this method is generally not suitable. In most cases, you need a method to compare a field and a field. Therefore, different objects representing the same data are equal.
The HashCode () method executes a hash function using the object content to generate an int value. Hashtable and HashMap use this value to determine which bucket (hash element) (or list) the key/value is in. Hashtable Performance
The main factor affecting hashtable efficacy is the average length of the table list, because the average search time is directly related to the average length. Obviously, to reduce the average length, you must increase the number of lists in hashtable. If the number of lists is so large that most or all lists contain only one record, you will get the best search efficiency. However, this may be too much. If your hashtable list is much larger than the number of data entries, you do not have to spend such memory. In some cases, this is not acceptable.
Ashtable and HashMap
There are three important differences between the Hashtable and HashMap classes. The first difference is mainly due to the historical reasons. Hashtable is based on the old Dictionary class, And HashMap isJava1.2 introducedMapAn Implementation of the interface.
Perhaps the most important difference is that the Hashtable method is synchronous, while the HashMap method is not. This means that, although you can use a Hashtable in a multi-threaded application without taking any special action, you must provide external synchronization for a HashMap. A convenient method is to use the static synchronizedMap () method of the Collections class to create a thread-safeMapObject and return it as an encapsulated object. The method of this object allows you to access the potential HashMap synchronously. The result is that when you do not need synchronization, you cannot cut off the synchronization in Hashtable (for example, in a single-threaded application), and synchronization increases a lot of processing costs.
The third difference is that only HashMap allows you to use a null value as the key or value of a table entry. Only one record in HashMap can be an empty key, but any number of entries can be empty values. That is to say, if no search key is found in the table, or if a search key is found but it is an empty value, get () returns null. If necessary, use the containKey () method to differentiate the two cases.