[JavaSE] HashSet, HashMap, and javasehashset
**************************************** ********************************* *** Original article: blog.csdn.net/clark_xu Xu changliang's column**************************************** ********************************SET
The set stores non-repeated elements. The set does not contain elements whose equal value is true. The hashSet and TreeSet are two common implementation classes of the set interface, use the hash algorithm and the binary sorting algorithm respectively. For example, write 20 random numbers not repeated within 100 to the set.
Set <Interger> hashset = new HashSet <Integer> ();
Random r = new Random ();
While (hashset. size () <10 ){
Hashset. add (r. nextInt (100 ));
}
The set Set object is different from the List object. The element does not correspond to the subscript. It cannot retrieve specific elements from the Set set, but can traverse the entire Set by iterator, returns the Iterator traversal object;
It can also be an enhanced For loop, which is equivalent in the java compiler;
For example:
Set <String> strset = new HashSet <String>;
Strset. add ("adb"); strset. add ("che ");
Enhanced for Loop
For (String str: strset ){
System. out. println (str)
}
The iterator is:
Iterator It = strset. iterator ();
While (It. hasNext ()){
String str = It. next ();
System. out. println (str)
}
6.1 HashSet and HashCode
The implementation of HashSet is implemented through the Hash table:
The process of adding an object to a HashSet:
Element-retrieve the hashcode of the element-index the corresponding bucket through the Hash algorithm;
Implementation of the contains method of HashSet:
Find the corresponding bucket for the hashcode value of the parameter object, and then compare it with the object of the space for equal;
HashCode method, consistent with the equals method; generally, the hashCode algorithm is generated using IDE tools;
Map set
The map set defines the query table, that is, the so-called "value" "key" ing pair. The key can be seen as the index of the value, and the Value as the Key cannot be repeated in the Set;
Implementation classes of map interfaces are commonly used HashMap implemented by hash tables and TreeMap sorted by binary trees;
Common map interface methods include get and set:
Object put (K key, Object value); writes the Key_value ing pair to the Map. If the set already contains the Key, the Value of the Key ing principle is returned; if not, null is returned.
V get (K key); returns the value mapped to the K value;
7.1 common Map Methods
Boolean containsKey (Object key); determines whether the map contains the Key index;
Boolean containsValue (object value); determines whether the map contains the value of value;
For example:
Create an array:
String str = "123,321,124,421,125,521 ";
String [] arr = str. split (",");
Create a Map object
Map <String, integer> map = new HashMap <String, integer> ();
Count the number of occurrences of characters: str is used as the Key and the number of occurrences is used as the Value. You need to traverse the character array and check whether the set contains the Key Value to determine the number of occurrences;
For (I = 0; I <arr. length; I ++ ){
If (map. containsValue (arr [I])
{/** Put method replacement, value + 1, key unchanged */
Map. put (arr [I], map. get (arr [I]) + 1 );
} Else {
/** Initial Value */
Map. add (arr [I], 1)
}
**************************************** ********************************* *** Original article: blog.csdn.net/clark_xu Xu changliang's column**************************************** ********************************