java HashMap details and example code

  • 2020-06-01 09:56:53
  • OfStack

java HashMap


/*
* Map Set characteristics 
*  The object that maps the key to the value, 1 Three maps cannot contain duplicate values; Each key can only be mapped to at most 1 A value 
* 
* Map The collection and Collection The difference between sets? 
* Map Collection storage elements come in pairs, Map The key of the collection is only 1 It's repeatable. This can be interpreted as: husband and wife 
* Collection Collection storage elements are present separately, Collection The son of Set Is the only 1 The, List "Bachelor" is repeatable. You can think of this as "bachelor" 
* 
*  Note: 
* Map The data structure value of the collection is valid for the key, and the limit value is invalid 
* Collection The data structure of the collection is valid for the elements 
* 
* Map Functional overview of the collection: 
* 1 : add function 
* V put(K key,V value);// Add elements 
*  If the bond is number one 1 Substore, store the element directly, and return null
*  If the key is not number one 1 Substore, replace the previous value with the value, and return the previous value 
* 
* 2 : delete function 
* void clear();// Removes all key-value pairs 
* V remove(Object key);// Delete the key-value pairs according to the key and return the value 
* 
* 3 : judgment function 
* boolean containsKey(Object key);// Determines whether the collection contains the specified key 
* boolean containsValue(Object value);// Determines whether the collection contains the specified value 
* boolean isEmpty();// Determines if the set is empty 
* 
* 4 : get function 
* set<Map,Entry<E,V>> entrySet(); Gets a collection of objects for key-value pairs 
* V get(Object key);// Get the value by the key 
* Set<K> keySet();// Gets a collection of all the keys in the collection 
* Collection<V> values();// Gets a collection of all the values in the collection 
* 
* 5 : length function 
* int size();// Returns the logarithm of a key-value pair in the collection 
* */

Traversal of the Map collection

Mode 1, query the value according to the key

Gets the collection of all keys

Walk through the collection of keys, getting each key

Query values by key

Mode 2 queries the key and value according to the object of the key-value pair

Gets a collection of objects for all key-value pairs

Traverses the collection of key-value pairs of objects, getting objects for each key-value pair

Query for keys and values based on the object of the key-value pair

Mode 1, query the value according to the key


/*

* Map The traversal of the collection, querying the value by the key 
* 
*  Ideas: 
* A: Get all the keys 
* B: Walk through the collection of keys, get each 1 A key 
* C: Query values by key 
* */

 


import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
/*
 * Map The traversal of the collection, querying the value by the key 
 *
 *  Ideas: 
 * A: Get all the keys 
 * B: Walk through the collection of keys, get each 1 A key 
 * C: Query values by key 
 * */
 
public class IntegerDemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
 
    Map<String, String> map = new HashMap<String, String>();
 
    map.put("hello", "world");
    map.put("java", "c++");
    map.put("sql", "os");
 
    System.out.println(map);
 
    // A: Get all the keys 
    Set<String> set = map.keySet();
 
    // B: Walk through the collection of keys, get each 1 A key 
    for (String key : set) {
      // C: Query values by key 
      String value = map.get(key);
      System.out.println(key + "---" + value);
    }
  }
}

Mode 2 queries the key and value according to the object of the key-value pair


/*
* Map The traversal of the collection, querying the keys and values based on the object 
* 
*  Ideas: 
* A: Gets a collection of all key-value pairs 
* B: Iterate through the collection of key-value pairs of objects, getting each 1 Object of a key value pair 
* C: Gets keys and values 
* */


import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
/*
 * Map The traversal of the collection, querying the keys and values based on the object 
 *
 *  Ideas: 
 * A: Gets a collection of all key-value pairs 
 * B: Iterate through the collection of key-value pairs of objects, getting each 1 Object of a key value pair 
 * C: Gets keys and values 
 * */
 
public class IntegerDemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
 
    Map<String, String> map = new HashMap<String, String>();
 
    map.put("hello", "world");
    map.put("java", "c++");
    map.put("sql", "os");
 
    System.out.println(map);
 
    // A: Gets a collection of all key-value pairs 
    Set<Map.Entry<String, String>> set = map.entrySet();
 
    // B: Iterate through the collection of key-value pairs of objects, getting each 1 Object of a key value pair 
    for (Map.Entry<String, String> me : set) {
      // C: Gets keys and values 
      String key = me.getKey();
      String value = me.getValue();
      System.out.println(key + "---" + value);
    }
  }
}
 


 



/*
* 1:HashMap and Hashtable The difference between? 
* HashMap Threads are not safe, efficient, allowed null The key and null value 
* Hashtable Thread safe, inefficient, not allowed null The key and null value 
* 
* 2:List . Set . Map Are all interfaces inherited from Map Interface? 
* List . Set Not inherited from Map Interfaces, which inherit from Collection interface 
* Map The interface itself is 1 Top level interface 
* */
import java.util.HashMap;
import java.util.Hashtable;
 
public class IntegerDemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
 
    HashMap<String, String> hm = new HashMap<String, String>();
    Hashtable<String, String> ht = new Hashtable<String, String>();
 
    hm.put("hello", "world");
    hm.put("java", "c++");
    hm.put(null, "sql");
 
    ht.put("hello", "world");
    ht.put("java", "c++");
    ht.put(null, "sql");// Exception in thread "main"
              // java.lang.NullPointerException
  }
}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: