HashSet
HashSet
HashSet
1. The values in HashSet
2. The capacity of HashSet
Constructor:
HashSet() The default equality comparator creates an empty new instance.
HashSet(IEnumerable
HashSet(IEqualityComparer
HashSet(IEnumerable
Because HashSet
The following is an introduction to some of its common methods
Member type description
Add Method adds the specified element to the set
Clear Method clears all elements in the set
Contains Method determines whether an element is in the HashSet
Exists Method determines whether the HashSet
ExceptWith Method removes all elements in the specified collection from the current HashSet
IntersectWith Method modifies the current HashSet
IsProperSubsetOf Method determines whether the HashSet
IsProperSupersetOf Method determines whether the HashSet
IsSunsetOf Method determines whether the HashSet
IsSupersetOf Method determines whether the HashSet
Remove Method removes the specified element from the HashSet
RemoveWhere Method removes the specified element from the HashSet
SetEquals Method determines whether the HashSet
SynmmetricExceptWith Method modifies the current HashSet
TrimExcess Method will HashSet
UnionWith Method
2. The key of Map and Set have one thing in common The characteristic is the uniqueness of the set. TreeMap has an additional sorting function.
Hashtables (hash tables) are not a new concept in the computer field. They were designed to speed up computer processing, which is very slow by today's standards, and they allow you to quickly find a particular entry when querying many data entries. Although modern machines are thousands of times faster, hashtables are still a useful tool to get the best performance from your applications.
Hashtable and HashMap objects allow you to combine a key and a value and input the key/value pair into the table using the put() method. You can then get the value by calling the get() method, passing the key as a parameter. Key and value can be any objects as long as they meet two basic requirements. Note that because keys and values must be objects, primitive types must be converted to objects using methods such as Integer(int).
In order to use an object of a specific class as a key, this class must provide two methods, equals() and hashCode(). These two methods are in java.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 overload these two methods yourself. method.
Equals () method compares its object with another object and returns true if the two objects represent the same information. This method also looks to make sure that both objects belong to the same class. Object.equals() returns true if the two reference objects are identical objects, which explains why this method is generally not a good fit. In most cases, you need a way to compare field by field, so we consider different objects representing the same data to be equal.
The HashCode() method generates an int value by performing a hash function using the contents of the object. Hashtable and HashMap use this value to figure out which bucket (or list) a key/value pair is in. Hashtable performance
The main factor affecting the efficiency of hashtable is the average length of the list in the table, because the average search time is directly related to this average length. Obviously, to reduce the average length, you must increase the number of lists in the hashtable; you will get the best search efficiency if the number of lists is so large that most or all lists contain only one record. However, this may be going too far. If your hashtable has far more lists than data entries, then there is no need for you to make such a memory cost, and in some cases, it is impossible for people to accept this approach.
ashtable and HashMap
The Hashtable and HashMap classes have three important differences. The first difference is mainly for historical reasons. Hashtable is based on the old Dictionary class, and HashMap is an implementation of the Map interface introduced in Java 1.2.
Perhaps the most important difference is that Hashtable's methods are synchronous, while HashMap's methods are not. This means that, although you can use a Hashtable in a multi-threaded application without taking any special action, you must also provide external synchronization for a HashMap. A convenient method is to use the static synchronizedMap() method of the Collections class, which creates a thread-safe Map object and returns it as an encapsulated object. This object's methods allow you to access the underlying HashMap synchronously. The result of this is that you can't cut off synchronization in the Hashtable when you don't need it (such as in a single-threaded application), and synchronization adds a lot of processing overhead.
The third difference is that only HashMap allows you to use null values as the key or value of a table entry. Only one record in a HashMap can be an empty key, but any number of entries can be an empty value. This means that if the search key is not found in the table, or if the search key is found but it is a null value, then get() will return null. If necessary, use the containKey() method to distinguish between these two situations.