Java basics of understanding --HashMap

java.util

Class HashMap<K,V>

  java.lang.Object

    java.util.AbstractMap <K, V>

      java.util.HashMap <K, V>

  Parameter Type

  K - type maintained by the Map key

  V - type of mapped values

public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable

A face questions:  

What HashMap of key deposit should pay attention to when the object?

    A: If your custom objects as Map keys, you must rewrite the hashCode and equals.

Face two questions:  

? HashCode role in HashMap
    A: 1, it returns the object hash value (hash Well), to support the hash table, such as HashMap

      2, can improve the performance of the hash table

Understanding of the HashMap?  

Map interface HashMap is based on a hash table to achieve, this implementation provides all of the optional Map operations, and allows the null value and the null key (roughly equivalent to the Hashtable HashMap class, except that it is not synchronized, and allow null value) .HashMap not guarantee the order stored map object, and it can not guarantee the order of the same period of time.

  One example of HashMap has two parameters affect its performance: initial capacity and the load factor is the capacity of the initial capacity when creating the hash table, until the load factor is allowed to increase the capacity of the hash table is automatically obtained metric is satisfied, when the bulk the number of entries in the list exceeds the initial capacity and load factor of the product, then the hash table will be re-hashed (that is, the internal structure was rebuilt), to expand the hash table twice. so if iteration performance is important, not the initial capacity is set too high (or too low load factor), the hash table is rebuilt as a waste of performance.

  Default initial capacity of 16, a default load factor is 0.75.

. 1  public  class HashMapDemo {
 2      public  static  void main (String [] args) {
 . 3          // Constructs an empty the HashMap 
. 4          the Map Map = new new the HashMap ();
 . 5          // default initial capacity of 16, a default load factor is 0.75 
. 6          the Map MAP1 = new new the HashMap (16, ( a float ) 0.75 );
 . 7      }
 . 8 }

  The method detailed map corresponding to:

. 1  public  class HashMapDemo {
 2      public  static  void main (String [] args) {
 . 3          // Constructs an empty the HashMap 
. 4          the Map Map = new new the HashMap ();
 . 5          // default initial capacity of 16, a default load factor is 0.75 
. 6          the Map MAP1 = new new the HashMap (16, ( a float ) 0.75 );
 . 7          map.put ( ". 1", "A" );
 . 8          map.put ( "2", "B" );
 . 9          map.put ( ". 3 "," C " );
 10          map.put (". 4 "," D " );
11         //Detail
 12 is          // 1.public int size () Returns the number of key-value mappings map 
13 is          System.out.println ( "Number:" + map.size ());
 14          // 2.public Boolean isEmpty ( ) If this map does not contain the key-value mapping, it returns true. 
15          System.out.println ( "Empty?" + Map.isEmpty ());
 16          // 3.public V GET (Object Key) Returns the specified key is mapped 
. 17          System.out.println ( "mapping value: "+ as map.get (". 1 " ));
 18 is          // 4.public Boolean containsKey (Object key) if this map contains the specified key mapping, it returns true. 
. 19          System.out.println ( "1 contains key mapping:" + map.containsKey ( "1" ));
 20 is          // 5.public V PUT (key K, V value) plus new mappings. If the map previously contained a mapping for this key, the old value is replaced.
21 is          map.put ( ". 5", "E" );
 22 is          // 6.public the putAll void (<? K ,? the extends the extends V> the Map m)
 23 is          // specified all mappings map copied to this map, the this mapping will replace any mappings mapped to any key in the currently specified map is. 
24          map1.put ( ". 1", "A" );
 25          map1.put ( "2", "B" );
 26 is          map1.put ( ". 3", "C" );
 27          map.putAll (MAP1);
 28          System.out.println ( "new map" as map.get + ( ". 1")); // a
 29          // 7.public V remove (Object key) delete key map specified (if present) from the map that . 
30          map.remove ( ". 5" );
));
 32          // 8.public void the Clear () to delete all maps from the map.
33 is          // map.clear ();
 34 is          // 9.public Boolean containsValue (Object value) 
35          System.out.println ( "Is there a value corresponding mapping Key" + map.containsValue ( "C" ));
 36          / / 10.public Set <K> keySet () returns a Set view of the keys contained in this map. 
37 [          the Set MapSet = map.keySet ();
 38 is          the Iterator Iterator = mapSet.iterator ();
 39          the while (iterator.hasNext ()) {
 40              System.out.println ( "Key of the set value" + Iterator.next ()) ;
 41          }
 42          //11.public Collection <V> values () Returns a Collection view of the values contained in this map. 
43 is          Collection Collection = map.values ();
 44 is          the Iterator valuesIterator = collection.iterator ();
 45          the while (valuesIterator.hasNext ()) {
 46 is              System.out.println ( "value of values:" + valuesIterator.next ()) ;
 47          }
 48          // 12.public V getOrDefault (Object key, V defaultValue) returns the specified key is mapped, if no default value is displayed 
49          System.out.println ( "If there is no key corresponding to the value:" + map .getOrDefault ( "8", "AA" ));
 50          // 13.public boolean the Remove (Object key, Object value) Removes the specified key and the corresponding value of the relationship, if the relationship does not correspond,
         System.out.println(map.remove("4","D"));
52         //14.public boolean replace(K key,V oldValue,V newValue) 替换指定key值对应的value值
53         map.replace("1","A","aaa");
54         //jdk8的forEach的用法
55         map.forEach((key,value)->{
56             System.out.println("key:"+key+" value:"+value);
57         });
58     }
59 }

 

Guess you like

Origin www.cnblogs.com/wk-missQ1/p/12512579.html