1. Set: Corresponds to the mathematical "set" concept, is the simplest kind of collection.
The set collection does not distinguish between the order of elements, so it does not record the order in which elements are added.
The set collection does not contain duplicate elements, that is, any two elements E1 and E2 have E1.equals (E2) =false and a maximum of one null element.
2. Implements the set interface the class HashSet
A strategy for detecting duplicate elements based on hashing: the element value in the HashSet has a corresponding relation (hash function) to the index position of the element in the set, which can be based on the element value and the corresponding relationship before inserting the element in the HashSet. Calculates the insertion position of this element in the HashSet, if the value of the element to be inserted is already present in this position (or around the position), it cannot be inserted.
Μ Construction Method
U hashset ()
U hashset (<E> c)
Μ Other methods
U Boolean contains (Object O) to determine if the specified element exists
the remaining method of inheriting the parent class is left. There is no get method in this class.
4. The first type of collection has a common feature: they store objects that are unary (linear), except that they are stored in a way that is different from the data structures used, with collection as the base class-encapsulating the insertion and deletion of linear tables.
The common feature of the second set is that the data they store is two yuan, called "key-value" pairs, they can be quickly based on a key word to get its corresponding value, which is called the keyword is because it must be unique, This ensures that the value obtained by the key is fixed each time, that is, the values that were last set.
both the list interface and the set interface are collection sub-interfaces
Implementing the List interface: storing data based on a linear list, such as Vector
Implement set interfaces: They do not allow duplicate elements, and the elements are unordered (the extracted elements are not the same as the order in which they were added); HashSet.
5. In Java, the "key-value" pairs of type objects, "keys"-index information, and "value"-corresponding to the index value are specifically established in Hashtable.
¯ Why do I use the data structure of the "key-value" pair
Μ Reason: Facilitate information retrieval, improve the speed of retrieving information in large amount of data.
Μ Working principle
(1) If you want to query the specified data from it, you have to iterate through the array, so the efficiency will be very low.
(2) Another way of thinking: storing 10 in an array is not inserted into the first free space.
Μ There is an index conflict problem: For a hash function, different values get the same "key", meaning that different objects may be stored at the same index position.
Μ Resolution Method
• use technical methods, such as designing hash functions that minimize conflict situations or specifying a response strategy when a conflict occurs;
• increase the capacity of the hash table, depending on the amount of data to be stored--to take the low probability of conflict by increasing the cost of space
new question: How to store the hash table storage space according to the amount of stored data.
è load factor: The ratio of actual objects in the hash table, 75% most suitable
6. "Key-value" pairs of the canonical--hashtable class
in the Java "key-value" Pair-type collection class, the implementation details of using hash function to optimize the data search efficiency and to deal with data conflicts in hash table are already encapsulated. Where Hashtable is a model of the "key-value" set class.
Μ Construction Method
U Hashtable ()
U Hashtable (int initialcapacity)
U Hashtable (int initialcapacity, floatloadfactor)
U Hashtable (<K,V> t)
Μ Other methods
U v put (K key, v value): Insert a "key-value" pair into the Hashtable object
U V get (Object key): According to key this "key" from the Hashtable object to retrieve the corresponding "value"
For example, Ht.put (new Integer (1), New String ("Tom");
Stringstr=ht.get (New Integer (1));
U boolean containskey (Object key)
Determines whether the key exists in the Hashtable object.
U boolean containsvalue (Object value)
Determine if "value" exists in Hashtable.
For example, the Boolean Flag=ht.containskey (New Integer (2));
flag=ht.containsvalue ("Rose");
U Public boolean contains (Object value) with Containsvalue () method.
U public void Clear ()
Empty this hashtable so that it does not contain any keys.
7. The enumerator is a tool used to access the collection elements, which not only provides several methods that can be used to access the collection, but also demonstrates the idea of resolving the "object type uncertainty" problem when accessing an object.
¯ access to collection classes "uncertainty" challenges
There are many different types of Java collection classes (such as vectors or lists) in Java, and programmers want to access the data in the same type of way.
¯ Enumerator Interface
The java.util.Iterator (enumerator interface) encapsulates the method of "no differential access to the collection object."
8. Related Methods
in each collection class (such as Vector or Hashtable), there is a iterator () method by which the collection objects can pass control over the class to the iterator interface.
in the iterator interface, the Boolean Hasnext () method is provided to determine whether the next element in the collection object can be obtained through the enumerator.
in the iterator interface, the E Next () method is provided to get the next element in the collection object, which returns a generic object.
9. Enumerator "Separation" thought
Μ the design idea of "traversing different kinds of set objects" using "separation";
Μ the traversal of this business action with the object (such as a collection) that will perform the traversal operation. On this basis, the common code that traverses different sets of objects is abstracted, and the function code is encapsulated into the enumerator interface, and the same set of code can be used to traverse different kinds of sets.
Μ is precisely because the enumerator separates the Business action (enumeration) and the data (the collection) to be manipulated by the business action, so it can be status quo.
10. When the package is poured, the specific class or * is the same, does not affect the operating rate.