YouTip LogoYouTip

Java Hashmap

[![Image 1: Java Collections Framework](#) Java Collections Framework](#) HashMap is a hash table that stores content as key-value pairs. HashMap implements the Map interface, stores data based on the key's HashCode value, has fast access speed, allows at most one record's key to be null, and does not support thread synchronization. HashMap is unordered, meaning it does not record the insertion order. HashMap inherits from AbstractMap and implements the Map, Cloneable, and java.io.Serializable interfaces. !(#) The key and value types of HashMap can be the same or different. You can use String type for both key and value, or use Integer type for key and String type for value. !(https://static.jyshare.com/images/mix/java-map.svg) The elements in HashMap are actually objects, and some common basic types can use their wrapper classes. The wrapper class table for basic types is as follows: | Basic Type | Reference Type | | --- | --- | | boolean | Boolean | | byte | Byte | | short | Short | | int | Integer | | long | Long | | float | Float | | double | Double | | char | Character | The HashMap class is located in the java.util package and needs to be imported before use. The syntax is as follows: import java.util.HashMap; // Import the HashMap class The following example creates a HashMap object Sites with Integer type key and String type value: HashMap Sites = new HashMap(); ### Add Elements The HashMap class provides many useful methods. You can use the put() method to add key-value pairs: ## Example // Import the HashMap class import java.util.HashMap; public class TutorialTest { public static void main(String[] args){ // Create a HashMap object Sites HashMap Sites =new HashMap(); // Add key-value pairs Sites.put(1, "Google"); Sites.put(2, "Tutorial"); Sites.put(3, "Taobao"); Sites.put(4, "Zhihu"); System.out.println(Sites); } } After executing the above code, the output is as follows: {1=Google, 2=Tutorial, 3=Taobao, 4=Zhihu} The following example creates a String type key and String type value: ## Example // Import the HashMap class import java.util.HashMap; public class TutorialTest { public static void main(String[] args){ // Create a HashMap object Sites HashMap Sites =new HashMap(); // Add key-value pairs Sites.put("one", "Google"); Sites.put("two", "Tutorial"); Sites.put("three", "Taobao"); Sites.put("four", "Zhihu"); System.out.println(Sites); } } After executing the above code, the output is as follows: {four=Zhihu, one=Google, two=Tutorial, three=Taobao} ### Access Elements We can use the get(key) method to get the value corresponding to the key: ## Example // Import the HashMap class import java.util.HashMap; public class TutorialTest { public static void main(String[] args){ // Create a HashMap object Sites HashMap Sites =new HashMap(); // Add key-value pairs Sites.put(1, "Google"); Sites.put(2, "Tutorial"); Sites.put(3, "Taobao"); Sites.put(4, "Zhihu"); System.out.println(Sites.get(3)); } } After executing the above code, the output is as follows: Taobao ### Remove Elements We can use the remove(key) method to delete the key-value pair corresponding to the key: ## Example // Import the HashMap class import java.util.HashMap; public class TutorialTest { public static void main(String[] args){ // Create a HashMap object Sites HashMap Sites =new HashMap(); // Add key-value pairs Sites.put(1, "Google"); Sites.put(2, "Tutorial"); Sites.put(3, "Taobao"); Sites.put(4, "Zhihu"); Sites.remove(4); System.out.println(Sites); } } After executing the above code, the output is as follows: {1=Google, 2=Tutorial, 3=Taobao} To remove all key-value pairs, you can use the clear method: ## Example // Import the HashMap class import java.util.HashMap; public class TutorialTest { public static void main(String[] args){ // Create a HashMap object Sites HashMap Sites =new HashMap(); // Add key-value pairs Sites.put(1, "Google"); Sites.put(2, "Tutorial"); Sites.put(3, "Taobao"); Sites.put(4, "Zhihu"); Sites.clear(); System.out.println(Sites); } } After executing the above code, the output is as follows: {} ### Calculate Size To calculate the number of elements in HashMap, you can use the size() method: ## Example // Import the HashMap class import java.util.HashMap; public class TutorialTest { public static void main(String[] args){ // Create a HashMap object Sites HashMap Sites =new HashMap(); // Add key-value pairs Sites.put(1, "Google"); Sites.put(2, "Tutorial"); Sites.put(3, "Taobao"); Sites.put(4, "Zhihu"); System.out.println(Sites.size()); } } After executing the above code, the output is as follows: 4 ### Iterate HashMap You can use for-each to iterate through the elements in HashMap. If you only want to get the key, you can use the keySet() method, and then you can get the corresponding value through get(key). If you only want to get the value, you can use the values() method. ## Example // Import the HashMap class import java.util.HashMap; public class TutorialTest { public static void main(String[] args){ // Create a HashMap object Sites HashMap Sites =new HashMap(); // Add key-value pairs Sites.put(1, "Google"); Sites.put(2, "Tutorial"); Sites.put(3, "Taobao"); Sites.put(4, "Zhihu"); // Output key and value for(Integer i : Sites.keySet()){ System.out.println("key: "+ i +" value: "+ Sites.get(i)); } // Return all values for(String value: Sites.values()){ // Output each value System.out.print(value +", "); } } } After executing the above code, the output is as follows: key: 1 value: Google key: 2 value: Tutorial key: 3 value: Taobao key: 4 value: ZhihuGoogle, Tutorial, Taobao, Zhihu, * * * ## Java HashMap Methods hashmap The common methods of Java HashMap are listed below: | Method | Description | | --- | --- | | [clear()](#) | Removes all key/value pairs from the hashMap | | [clone()](#) | Copies the hashMap | | [isEmpty()](#) | Checks if the hashMap is empty | | [size()](#) | Returns the number of key/value pairs in the hashMap | | [put()](
← Java HashsetLinux Comm Ip β†’