1. Overview
The concept of set in Java is the same as that in mathematics. The biggest feature of set is that it does not allow repeated elements to be stored in it. Based on this feature, we can use the set interface to meet the storage requirements of the product categories mentioned above. Set can be used to filter elements stored in other sets to obtain a new set that does not contain duplicates.
2. Common Methods
According to the definition, the set interface inherits the collection interface and does not allow repeated items in the set. All original methods are ready-made and no new methods are introduced. The specific set implementation class depends on the equals () method of the added object to check equality.
Description of the functions of each method:
Public int size (): return the number of elements in the set. If the number of elements contained in the set is greater than integer. max_value, return integer. max_value;
Public Boolean isempty (): returns true if the set contains no elements;
Public Boolean contains (Object O): returns true if set contains the specified element.
;
Public iterator ()
: Return the iterator of the element in the set. The element returns no specific sequence unless the set improves the instance of certain classes to be guaranteed.
;
Public Boolean add (Object O): If the specified element does not exist in the Set, add it to the set.
;
Public Boolean remove (Object O): If a specified element exists in the set, it is deleted from the set.
;
Public Boolean removeall (collection C): If the set contains the specified set, all elements of the specified set are deleted from the set.
;
Public void clear (): deletes all elements from the set;
3. Principle Analysis
There is no relationship between the order in which hashset elements are stored and the order in which they are added. hashset maintains the order in which elements are added. treeset stores elements in our set.
In general, the treeset implementation is useful when elements are extracted in an orderly manner from a set. Add to treeset for smooth operation
The elements must be sortable. Similarly, comparable needs to be implemented for class objects added to the treeset.
Interface Support. For the comparable interface implementation. Suppose a tree knows how to keep java. Lang
The ordered state of the package class elements. In general, add the element to the hashset first, and then convert the set to the treeset
To perform sequential traversal faster. This is very similar to the use of hashmap.
In fact, the implementation principle of set is based on map. The use of many implementation classes in set is very similar to that in map. In map
The "key" cannot be repeated. This is not consistent with the elements in the set. In fact, the set uses the feature that the "key" in map cannot be repeated.
The clever Implementation of hashset is to create a "key-Value Pair", "key" is the object we want to store, and "value" is a constant. This ensures that,
The information we need to store is the "key ". The "key" cannot be repeated in map, which ensures that all elements stored in the set are not repeated. To determine whether the element is successfully added
Determine whether the "key-Value Pair" We saved to the map already exists. If yes, the return value must be a constant: present.
. If it does not exist, null indicates that the value is successfully added.
Share: