Hash Map Detailed explanation and examples


Application and examples HashMap in java

HashMap from java portion is set from 1.2, it provides a basic map java mapping interface. By storing data in (Key, Value) it is in the key-value pair. I want to get to know the value of the corresponding key. The reason to use HashMap named because it uses a technology called Hashing, Hashing is a long string can be converted into the same short string and a string. Benefits segment strings is faster and index marks. Also occasionally use HashSet HashMap, occasionally using a linked list to store key-value pairs.

HashMap several important attributes:

  • HashMap is part of the package java.util
  • HashMap integrated child abstract class AbstractMap, and AbstractMap Map interface also provides a non-fetched.
  • It also implements the sequence can be cloned and interfaces, K and V in the foregoing description represent Key and Value
  • Duplicate Keys allowed HashMap but allowing duplicate Values, i.e., a single key can not have more value, but more than one key may comprise a same value.
  • The only empty HashMap allow more space but allows the Key Values
  • This class does not guarantee the order of the mapping, it can not guarantee the order has remained the same, and it HashTable similar, but not synchronized.

all image

HashMap internal structure

Internal HashMap contains a node array, wherein a node may be represented as a class contains four fields.

  1. int hash
  2. K key
  3. V value
  4. Node<K,V> next

Node contains a reference to the object itself, so this is a linked list.
HashMap:
all image

HashMap performance

  1. Initial capacity (Initial Capacity)
  2. Load factor (Load Factor)
    • \ (Load factor = \ {the number of elements in the table stored in the hash table size} cfrac {the} \)
    • Example: If the internal capacity of 16, the load factor is 0.75, then when the table has 12 elements, bucket number will automatically increase.

I.e., the amount of container carrying capacity, i.e. once instantiated HashMap with the initial capacity . Load factor measured using need to stop when to re-hash. The step of re-hashing is used to enhance the capacity. In the HashMap capacity is multiplied by two. The load factor is also a measure to allow those parts to be filled before re-hash. When the number of entries in the HashMap product increases and the load factor of the current capacity, the capacity will increase, then it was re-hashed. If the initial capacity continues to increase so does not stop with re-hash, but by constantly increased initial capacity it increases the complexity of iterative time, so he should be carefully chosen to enhance his performance, when setting the initial capacity it should be expected values into account. The load factor is set to 0.75 Typically, it is a good balance of space and time spent. Load coefficient value between 0-1

Synchronized HashMap

That is HashMap non-synchronized multiple threads can simultaneously access. If multiple threads are accessing the same class and at least one thread to change his structure so it is necessary for him externally synchronized. It is an object through synchronous mapping of some of the package to complete. If such an object does not exist, it can be encapsulated in the Collections.synchronizedMap () in order to make the HashMap synchronization, and to avoid accidental unsynchronized access.
Examples are as follows:

Map m = Collections.synchronizedMap(new HashMap(...));Map 

Now is the synchronization of the Map m

If you make any structural modifications (except through the iterator's remove method any way) after creating the iterator class iterators are quick failure. When the iterator fails, it will throw a ConcurrentModificationException.

HashMap constructor

HashMap provides four constructors, each access modifier are public:

  1. HashMap (): which is the default constructor creates HashMap instance, an initial capacity of 16, a load factor of 0.75.
  2. HashMap (int initial capacity): It creates a HashMap instance, the instance with the specified initial capacity and the load factor of 0.75.
  3. HashMap (int initial capacity, float loadFactor): It creates a HashMap instance, the instance with the specified initial capacity and the specified load factor.
  4. HashMap (Map Map): It uses the same mapping to create the specified mapping HashMap instance.

Example:

    // Java program to illustrate 
    // Java.util.HashMap 
  
    import java.util.HashMap; 
    import java.util.Map; 
    
    public class GFG { 
        public static void main(String[] args) 
        { 
  
        HashMap<String, Integer> map 
            = new HashMap<>(); 
  
        print(map); 
        map.put("vishal", 10); 
        map.put("sachin", 30); 
        map.put("vaibhav", 20); 
  
        System.out.println("Size of map is:-"+ map.size()); 
  
        print(map); 
        if (map.containsKey("vishal")) { 
            Integer a = map.get("vishal"); 
            System.out.println("value for key"+ " \"vishal\" is:- "+ a); 
        } 
  
        map.clear(); 
        print(map); 
    } 
  
    public static void print(Map<String, Integer> map) 
    { 
        if (map.isEmpty()) { 
            System.out.println("map is empty"); 
        } 
  
        else { 
            System.out.println(map); 
        } 
    } 
} 

Output:

map is empty  
Size of map is:- 3  
{vaibhav=20, vishal=10, sachin=30}  
value for key "vishal" is:- 10  
map is empty

HashMap time complexity

Providing HashMap constant time complexity of the basic operations, if the hash function properly prepared and correctly distributed among the various elements in buckets, then use the get and put. Through all the HashMap HashMap capacity and depend on a number of key - value pairs. Generally speaking, it is proportional to the size and capacity +. HashMap capacity of buckets. So a large number of reservations began in the bucket unfriendly HashMap.

HashMap method

1. void clear (): Removes all mappings from the map.

  • Syntax Hash_Map.clear ()
  • Parameter: Void
  • Return value: None Return Value
  • Examples are as follows:
 //将字符串映射成为整数键
 // Java code to illustrate the clear() method 
 import java.util.*; 

 public class Hash_Map_Demo { 
     public static void main(String[] args) 
     { 

         // Creating an empty HashMap 
         HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 
 
         // Mapping string values to int keys 
         hash_map.put(10, "pomelos"); 
         hash_map.put(15, "4"); 
         hash_map.put(20, "pomelos"); 
         hash_map.put(25, "Welcomes"); 
         hash_map.put(30, "You"); 
 
         // Displaying the HashMap 
         System.out.println("Initial Mappings are: " + hash_map); 
 
         // Clearing the hash map using clear() 
         hash_map.clear(); 
 
         // Displaying the final HashMap 
         System.out.println("Finally the maps look like this: " + hash_map); 
       } 
   } 

Output:

Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
Finally the maps look like this: {}
//将整数映射成为字符串
// Java code to illustrate the clear() method 
import java.util.*; 

public class Hash_Map_Demo { 
    public static void main(String[] args) 
    { 

        // Creating an empty HashMap 
        HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

        // Mapping int values to string keys 
        hash_map.put("pomelos", 10); 
        hash_map.put("4", 15); 
        hash_map.put("pomelos", 20); 
        hash_map.put("Welcomes", 25); 
        hash_map.put("You", 30); 

        // Displaying the HashMap 
        System.out.println("Initial Mappings are: " + hash_map); 

        // Clearing the hash map using clear() 
        hash_map.clear(); 

        // Displaying the final HashMap 
        System.out.println("Finally the maps look like this: " + hash_map); 
    } 
} 

Output:

Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
Finally the maps look like this: {}

2. boolean containsKey (key_element) inquire whether there is a specified key mapping

  • Syntax Hash_Map.containsKey (key_element)
  • Parameters: Only key_element parameter points in the map you want to map the elements of the query.
  • Return Value: The return value only ture and false
  • Examples are as follows:
 //将字符串映射为整数
 // Java code to illustrate the containsKey() method 
 import java.util.*; 
 
 public class Hash_Map_Demo { 
     public static void main(String[] args) 
     { 

     // Creating an empty HashMap 
     HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

     // Mapping string values to int keys 
     hash_map.put(10, "pomelos"); 
     hash_map.put(15, "4"); 
     hash_map.put(20, "pomelos"); 
     hash_map.put(25, "Welcomes"); 
     hash_map.put(30, "You"); 

     // Displaying the HashMap 
     System.out.println("Initial Mappings are: " + hash_map); 

     // Checking for the key_element '20' 
     System.out.println("Is the key '20' present? " +  
     hash_map.containsKey(20)); 

     // Checking for the key_element '5' 
     System.out.println("Is the key '5' present? " +  
     hash_map.containsKey(5)); 
     } 
 } 

Output:

Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
 Is the key '20' present? true
 Is the key '5' present? false
//将整数映射成为字符串
// Java code to illustrate the containsKey() method 
 import java.util.*; 
 
 public class Hash_Map_Demo { 
     public static void main(String[] args) 
     { 

     // Creating an empty HashMap 
     HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

     // Mapping int values to string keys 
     hash_map.put("pomelos", 10); 
     hash_map.put("4", 15); 
     hash_map.put("pomelos", 20); 
     hash_map.put("Welcomes", 25); 
     hash_map.put("You", 30); 

     // Displaying the HashMap 
     System.out.println("Initial Mappings are: " + hash_map); 

     // Checking for the key_element 'Welcomes' 
     System.out.println("Is the key 'Welcomes' present? " +  
     hash_map.containsKey("Welcomes")); 

     // Checking for the key_element 'World' 
     System.out.println("Is the key 'World' present? " +  
     hash_map.containsKey("World")); 
     } 
 } 

Output:
Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25} Is the key 'Welcomes' present? true Is the key 'World' present? false

3. boolean containsValue (Object value): a value for a particular key to delete any mappings

  • 语法:Hash_Map.containsValue(Object Value)
  • Parameters: This method takes one parameter value of the object type, and reference should be mapped by checking the value of any key within the mapping.
  • Return Value: If the mapping value is detected, then the method returns a boolean value of true, the remaining cases are false.
  • Time complexity: O (n)
  • Examples are as follows:
// 将字符串映射为整数
// Java code to illustrate the containsValue() method 
 import java.util.*; 
 
 public class Hash_Map_Demo { 
     public static void main(String[] args) 
     { 

     // Creating an empty HashMap 
     HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

     // Mapping string values to int keys 
     hash_map.put(10, "pomelos"); 
     hash_map.put(15, "4"); 
     hash_map.put(20, "pomelos"); 
     hash_map.put(25, "Welcomes"); 
     hash_map.put(30, "You"); 

     // Displaying the HashMap 
     System.out.println("Initial Mappings are: " + hash_map); 

     // Checking for the Value 'pomelos' 
     System.out.println("Is the value 'pomelos' present? " +  
     hash_map.containsValue("pomelos")); 

     // Checking for the Value 'World' 
     System.out.println("Is the value 'World' present? " +  
     hash_map.containsValue("World")); 
     } 
 } 

Output:

Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
 Is the value 'pomelos' present? true
 Is the value 'World' present? false
// 经整数映射为字符串
// Java code to illustrate the containsValue() method 
 import java.util.*; 
 
 public class Hash_Map_Demo { 
     public static void main(String[] args) 
     { 

     // Creating an empty HashMap 
     HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

     // Mapping int values to string keys 
     hash_map.put("pomelos", 10); 
     hash_map.put("4", 15); 
     hash_map.put("pomelos", 20); 
     hash_map.put("Welcomes", 25); 
     hash_map.put("You", 30); 

     // Displaying the HashMap 
     System.out.println("Initial Mappings are: " + hash_map); 

     // Checking for the Value '10' 
     System.out.println("Is the value '10' present? " + 
     hash_map.containsValue(10)); 

     // Checking for the Value '30' 
     System.out.println("Is the value '30' present? " + 
     hash_map.containsValue(30)); 

     // Checking for the Value '40' 
     System.out.println("Is the value '40' present? " +  
     hash_map.containsValue(40)); 
     } 
 } 

Output:

 Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
 Is the value '10' present? false
 Is the value '30' present? true
 Is the value '40' present? false

4. Object clone (): It is used to return a shallow copy of the above-described hash map

  • Syntax Hash_Map.clone ()
  • Parameter: Void
  • Returns: This method returns only one copy of the HashMap
  • Examples are as follows:
  // 将字符串映射为数字
  // Java code to illustrate the clone() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Displaying the cloned HashMap using clone() 
      System.out.println("The cloned map look like this: " + hash_map.clone()); 
      } 
  } 

Output:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The cloned map look like this: {25=Welcomes, 10=pomelos, 20=pomelos, 30=You, 15=4}
  // 将整数映射为字符串
  // Java code to illustrate the clone() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Displaying the cloned HashMap using clone() 
      System.out.println("The cloned map look like this: " + hash_map.clone()); 
      } 
  } 

Output:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The cloned map look like this: {pomelos=20, 4=15, You=30, Welcomes=25}

5. boolean isEmpty (): returns a collection of hash map view

  • Syntax Hash_Map.isEmpty ()
  • Parameter: Void
  • Return Value: If the map is empty or does not contain any mapping, the method returns a boolean value of true, otherwise it is false.
  • Examples are as follows:
  // 将整数映射成为字符串
  // Java code to illustrate the isEmpty() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("The Mappings are: " + hash_map); 

      // Checking for the emptiness of Map 
      System.out.println("Is the map empty? " + hash_map.isEmpty()); 
      } 
  } 

Output:

  The Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  Is the map empty? false     
  // 对于空hashMap
  // Java code to illustrate the isEmpty() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Displaying the HashMap 
      System.out.println("The Mappings are: " + hash_map); 

      // Checking for the emptiness of Map 
      System.out.println("Is the map empty? " + hash_map.isEmpty()); 
      } 
  } 

Output:

  The Mappings are: {}
  Is the map empty? true

6. Set entrySet (): Returns the hash map Set view

  • Syntax hash_map.entrySet ()
  • Parameter: Void
  • Return Value: This method returns the hash map has the same set of elements.
  • Example:
  // 字符串映射成整数
  // Java code to illustrate the entrySet() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using entrySet() to get the set view 
      System.out.println("The set is: " + hash_map.entrySet()); 
      } 
  } 

Output:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The set is: [20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4]
// 讲整数映射成为字符串
// Java code to illustrate the entrySet() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using entrySet() to get the set view 
      System.out.println("The set is: " + hash_map.entrySet()); 
      } 
  } 

Output:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The set is: [4=15, pomelos=20, You=30, Welcomes=25]

7. Object get (Object key): value map used to retrieve or obtain from a specific key

  • Syntax hash_map.keySet ()
  • Parameters: No parameter
  • Return Value: This method returns a collection of hash map bond.
  • Examples are as follows:
// 将字符串映射为整数值
// Java code to illustrate the keySet() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 
  
      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using keySet() to get the set view of keys 
      System.out.println("The set is: " + hash_map.keySet()); 
      } 
  } 

Output:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The set is: [20, 25, 10, 30, 15]
// 将整数映射成为字符串
// Java code to illustrate the keySet() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using keySet() to get the set view of keys 
      System.out.println("The set is: " + hash_map.keySet()); 
      } 
  } 

Output:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The set is: [4, pomelos, You, Welcomes]

8. Set ketSet (): the key is used to return a set of view

* 语法 hash_map.keySet()
* 参数: 无参数
* 返回值:该方法返回一个具有散列映射键的集合。
* 示例如下:
// 将字符串映射为整数
// Java code to illustrate the keySet() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using keySet() to get the set view of keys 
      System.out.println("The set is: " + hash_map.keySet()); 
  } 
} 

Output:

    Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
    The set is: [20, 25, 10, 30, 15]
//将整数映射为字符串
// Java code to illustrate the keySet() method 
import java.util.*; 
  
public class Hash_Map_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty HashMap 
        HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 
  
        // Mapping int values to string keys 
        hash_map.put("pomelos", 10); 
        hash_map.put("4", 15); 
        hash_map.put("pomelos", 20); 
        hash_map.put("Welcomes", 25); 
        hash_map.put("You", 30); 
  
        // Displaying the HashMap 
        System.out.println("Initial Mappings are: " + hash_map); 
  
        // Using keySet() to get the set view of keys 
        System.out.println("The set is: " + hash_map.keySet()); 
    } 
} 

Output:

    Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
    The set is: [4, pomelos, You, Welcomes]

9. int size (): It is used to return the size of the map

* 语法: Hash_Map.size()
* 参数: 无需参数
* 返回值: 该方法返回映射的大小,这也表示映射中存在的键值对的数量。
* 示例如下
//将字符串映射成为整数
// Java code to illustrate the size() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Displaying the size of the map 
      System.out.println("The size of the map is " + hash_map.size()); 
      } 
  } 

Output:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The size of the map is 5
  // 将整数映射成为字符串
  // Java code to illustrate the size() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Displaying the size of the map 
      System.out.println("The size of the map is " + hash_map.size()); 
      } 
  } 

Output:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The size of the map is 4

10. Object put (Object key, Object value): for mapping a particular key-value pair is inserted into the map.

  • 语法 Hash_Map.put(key, value)
  • Parameters: This method has two parameters, object types are HashMap.
    • key: This refers to the key element that needs to be inserted into the Map for mapping.
    • value: This refers to the value that the above key would map into.
  • Return Value: If you pass a conventional key, the previous value is returned. If you pass a new peer, NULL is returned.
  • Examples are as follows:
// 当传递一个存在key
// Java code to illustrate the put() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Inserting existing key along with new value 
      String returned_value = (String)hash_map.put(20, "All"); 

      // Verifying the returned value 
      System.out.println("Returned value is: " + returned_value); 

      // Displayin the new map 
      System.out.println("New map is: " + hash_map); 
      } 
  } 

Output:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  Returned value is: pomelos
  New map is: {20=All, 25=Welcomes, 10=pomelos, 30=You, 15=4}
// 当传递一个新值
// Java code to illustrate the put() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 

      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 

      // Mapping string values to int keys 
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Inserting existing key along with new value 
      String returned_value = (String)hash_map.put(50, "All"); 

      // Verifying the returned value 
      System.out.println("Returned value is: " + returned_value); 

      // Displayin the new map 
      System.out.println("New map is: " + hash_map); 
      } 
  } 

Output:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  Returned value is: null
  New map is: {50=All, 20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}

11. putAll (Map M): It is used to copy all the elements of a map to another map.

  • 语法 new_hash_map.putAll(exist_hash_map)
  • Parameters: This method accepts a parameter exist_hash_map, the argument refers to an existing map we want to copy.
  • Return value: None Return Value
  • Exception: If we want to copy the mapping is NULL, this method throws NullPointerException.
  • Examples are as follows:
      // 将字符串映射为整数
      // Java code to illustrate the putAll() method 
      import java.util.*; 
      
      public class Hash_Map_Demo { 
      public static void main(String[] args) { 
          
      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 
  
      // Mapping string values to int keys  
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 
  
      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 
  
      // Creating a new hash map and copying 
      HashMap<Integer, String> new_hash_map = new HashMap<Integer, String>(); 
      new_hash_map.putAll(hash_map); 
  
      // Displaying the final HashMap 
      System.out.println("The new map looks like this: " + new_hash_map); 
      } 
  } 

Output:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The new map looks like this: {25=Welcomes, 10=pomelos, 20=pomelos, 30=You, 15=4}
// 将整数映射成为字符串
// Java code to illustrate the putAll() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 
  
      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Creating a new hash map and copying 
      HashMap<String, Integer> new_hash_map = new HashMap<String, Integer>(); 
      new_hash_map.putAll(hash_map); 

      // Displaying the final HashMap 
      System.out.println("The new map looks like this: " + new_hash_map); 
      } 
  } 

Output:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The new map looks like this: {pomelos=20, 4=15, You=30, Welcomes=25}

12. Object remove (Object key): It is a value for a particular key to delete any map.

  • 语法 Hash_Map.remove(Object key)
  • Parameters: This method uses a parameter key to delete it from the map of the map.
  • Return Value: If the key is present, the method returns the value that was previously mapped to the specified key, otherwise return NULL.
  • Examples are as follows:
  // 当传递一个已存在key
  // Java code to illustrate the remove() method 
      import java.util.*; 
      
      public class Hash_Map_Demo { 
      public static void main(String[] args) { 
              
      // Creating an empty HashMap 
      HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 
  
      // Mapping string values to int keys  
      hash_map.put(10, "pomelos"); 
      hash_map.put(15, "4"); 
      hash_map.put(20, "pomelos"); 
      hash_map.put(25, "Welcomes"); 
      hash_map.put(30, "You"); 
  
      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map);  
  
      // Removing the existing key mapping 
      String returned_value = (String)hash_map.remove(20); 
  
      // Verifying the returned value 
      System.out.println("Returned value is: "+ returned_value); 
  
      // Displayin the new map 
      System.out.println("New map is: "+ hash_map); 
      } 
  } 

Output:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  Returned value is: pomelos
  New map is: {25=Welcomes, 10=pomelos, 30=You, 15=4}
// 当传递一个新key
// Java code to illustrate the remove() method 
  import java.util.*; 
      
  public class Hash_Map_Demo { 
  public static void main(String[] args) { 
          
    // Creating an empty HashMap 
    HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 
  
    // Mapping string values to int keys  
    hash_map.put(10, "pomelos"); 
    hash_map.put(15, "4"); 
    hash_map.put(20, "pomelos"); 
    hash_map.put(25, "Welcomes"); 
    hash_map.put(30, "You"); 
 
    // Displaying the HashMap 
    System.out.println("Initial Mappings are: " + hash_map);  
 
    // Removing the new key mapping 
    String returned_value = (String)hash_map.remove(50); 
 
    // Verifying the returned value 
    System.out.println("Returned value is: "+ returned_value); 
 
    // Displayin the new map 
    System.out.println("New map is: "+ hash_map); 
 } 
} 

Output:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 3`0=You, 15=4}
  Returned value is: null
  New map is: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}

13. Collection values ​​(): It is used to return a set of values ​​in view of the HashMap.

  • Syntax Hash_Map.values ​​()
  • Parameter: Void
  • Return Value: This method returns a collection of view that includes all values ​​mapped.
  • Examples are as follows:
  // 将字符串映射为整数
  // Java code to illustrate the values() method 
  import java.util.*; 
  
  public class Hash_Map_Demo { 
      public static void main(String[] args) 
      { 
  
          // Creating an empty HashMap 
          HashMap<Integer, String> hash_map = new HashMap<Integer, String>(); 
  
          // Mapping string values to int keys 
          hash_map.put(10, "pomelos"); 
          hash_map.put(15, "4"); 
          hash_map.put(20, "pomelos"); 
          hash_map.put(25, "Welcomes"); 
          hash_map.put(30, "You"); 
  
          // Displaying the HashMap 
          System.out.println("Initial Mappings are: " + hash_map); 
  
          // Using values() to get the set view of values 
          System.out.println("The collection is: " + hash_map.values()); 
      } 
  }   

Output:

  Initial Mappings are: {20=pomelos, 25=Welcomes, 10=pomelos, 30=You, 15=4}
  The collection is: [pomelos, Welcomes, pomelos, You, 4]
// 将整数映射成字符串
// Java code to illustrate the values() method 
import java.util.*; 

public class Hash_Map_Demo { 
  public static void main(String[] args) 
  { 

      // Creating an empty HashMap 
      HashMap<String, Integer> hash_map = new HashMap<String, Integer>(); 

      // Mapping int values to string keys 
      hash_map.put("pomelos", 10); 
      hash_map.put("4", 15); 
      hash_map.put("pomelos", 20); 
      hash_map.put("Welcomes", 25); 
      hash_map.put("You", 30); 

      // Displaying the HashMap 
      System.out.println("Initial Mappings are: " + hash_map); 

      // Using values() to get the set view of values 
      System.out.println("The collection is: " + hash_map.values()); 
  } 
} 

Output:

  Initial Mappings are: {4=15, pomelos=20, You=30, Welcomes=25}
  The collection is: [15, 20, 30, 25]

references:

https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
https://www.geeksforgeeks.org/hashset-in-java/

Guess you like

Origin www.cnblogs.com/Pomelos/p/11959647.html