Java HashMap
A HashMap is a hash table that stores a key-value mapping.
HashMap implements the Map interface, stores data according to the HashCode value of the key, has fast access speed, allows a maximum of one record key to be null, and does not support thread synchronization.
The HashMap is unordered, that is, the order of the inserts is not recorded.
HashMap extends AbstractMap and implements Map, Cloneable, java.io.Serializable interfaces.
The key and value types of a HashMap can be the same or different. They can be String keys and value, or Integer keys and String value.
Elements in a HashMap are actually objects, and some common primitive types can use their wrapper classes.
The table of packing classes corresponding to basic types is as follows:
Basic types of | Reference types |
---|---|
boolean | Boolean |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
The HashMap class is in the java.util package and needs to be imported before use. The syntax is as follows:
import java.util.HashMap; // Introduce the HashMap class
Copy the code
Here we create a HashMap object named Sites, an Integer key, and a String value:
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
Copy the code
Add elements
The HashMap class provides many useful methods for adding key-value pairs using the put() method:
The instance
// Introduce the HashMap class
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// Create HashMap object Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// Add key-value pairs
Sites.put(1."Google");
Sites.put(2."Runoob");
Sites.put(3."Taobao");
Sites.put(4."Zhihu"); System.out.println(Sites); }}Copy the code
After executing the above code, the output is as follows:
{1=Google, 2=Runoob, 3=Taobao, 4=Zhihu}
Copy the code
Create a String key and a String value:
The instance
// Introduce the HashMap class
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// Create HashMap object Sites
HashMap<String, String> Sites = new HashMap<String, String>();
// Add key-value pairs
Sites.put("one"."Google");
Sites.put("two"."Runoob");
Sites.put("three"."Taobao");
Sites.put("four"."Zhihu"); System.out.println(Sites); }}Copy the code
After executing the above code, the output is as follows:
{four=Zhihu, one=Google, two=Runoob, three=Taobao}
Copy the code
Access to the elements
We can use the get(key) method to get the value corresponding to the key:
The instance
// Introduce the HashMap class
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// Create HashMap object Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// Add key-value pairs
Sites.put(1."Google");
Sites.put(2."Runoob");
Sites.put(3."Taobao");
Sites.put(4."Zhihu");
System.out.println(Sites.get(3)); }}Copy the code
After executing the above code, the output is as follows:
Taobao
Copy the code
Remove elements
We can use the remove(key) method to remove the key-value pairs corresponding to the key:
The instance
// Introduce the HashMap class
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// Create HashMap object Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// Add key-value pairs
Sites.put(1."Google");
Sites.put(2."Runoob");
Sites.put(3."Taobao");
Sites.put(4."Zhihu");
Sites.remove(4); System.out.println(Sites); }}Copy the code
After executing the above code, the output is as follows:
{1=Google, 2=Runoob, 3=Taobao}
Copy the code
To delete all key-value pairs, use the clear method:
The instance
// Introduce the HashMap class
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// Create HashMap object Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// Add key-value pairs
Sites.put(1."Google");
Sites.put(2."Runoob");
Sites.put(3."Taobao");
Sites.put(4."Zhihu"); Sites.clear(); System.out.println(Sites); }}Copy the code
After executing the above code, the output is as follows:
{}
Copy the code
Calculate the size
To count the number of elements in a HashMap, use the size() method:
The instance
// Introduce the HashMap class
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// Create HashMap object Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// Add key-value pairs
Sites.put(1."Google");
Sites.put(2."Runoob");
Sites.put(3."Taobao");
Sites.put(4."Zhihu"); System.out.println(Sites.size()); }}Copy the code
After executing the above code, the output is as follows:
4
Copy the code
Iterative HashMap
You can use for-each to iterate over elements in a HashMap.
You can use keySet() if you just want to get a key, or values() if you just want to get a value.
The instance
// Introduce the HashMap class
import java.util.HashMap;
public class RunoobTest {
public static void main(String[] args) {
// Create HashMap object Sites
HashMap<Integer, String> Sites = new HashMap<Integer, String>();
// Add key-value pairs
Sites.put(1."Google");
Sites.put(2."Runoob");
Sites.put(3."Taobao");
Sites.put(4."Zhihu");
// Outputs key and value
for (Integer i : Sites.keySet()) {
System.out.println("key: " + i + " value: "+ Sites.get(i)); }}}Copy the code
After executing the above code, the output is as follows:
key: 1 value: Google
key: 2 value: Runoob
key: 3 value: Taobao
key: 4Value: Zhihu Output key andvalue
for (Integer i : Sites.keySet()) {
System.out.println("key: " + i + " value: "+ Sites.get(i)); }}}Copy the code
After executing the above code, the output is as follows:
key: 1 value: Google
key: 2 value: Runoob
key: 3 value: Taobao
key: 4 value: Zhihu
Copy the code