Java HashMap集合【笔记】

2022-07-01 14:38:15 浏览数 (1)

标语: 追着光,靠近光,成为光,然后散发光!

今天给大家分享【Java HashMap集合】。

一、HashMap集合的概念:

HashMap 继承于AbstractMap,实现了 Map、Cloneable、java.io.Serializable 接口。

HashMap 根据键的 HashCode 值存储数据,具有很快的访问速度,最多允许一条记录的键为 null,不支持线程同步。

二、HashMap添加元素:

使用 对象名.put()方法

代码语言:javascript复制
import java.util.HashMap;

public class ChildrenTest {
    public static void main(String[] args) {
        HashMap<Integer, String> family = new HashMap<Integer, String>();
        // 添加键值对
        family.put(1, "Grandpa");
        family.put(2, "Grandma");
        family.put(3, "Father");
        family.put(4, "Mother");
        family.put(5, "Children");
        System.out.println(family);
    }
}

执行以上实例,输出结果为:

代码语言:javascript复制
{1=Grandpa, 2=Grandma, 3=Father, 4=Mother,5=Children}

三、HashMap访问元素:

使用 对象名.get(key)方法

代码语言:javascript复制
import java.util.HashMap;

public class ChildrenTest {
public static void main(String[] args) {
        HashMap<Integer, String> family = new HashMap<Integer, String>();
         // 添加键值对
        family.put(1, "Grandpa");
        family.put(2, "Grandma");
        family.put(3, "Father");
        family.put(4, "Mother");
        family.put(5, "Children");
        System.out.println(family.get(5));
    }
}

执行以上实例,输出结果为:

代码语言:javascript复制
Children

四、HashMap删除元素:

使用 对象名.remove(key)方法

代码语言:javascript复制
import java.util.HashMap;

public class ChildrenTest {
public static void main(String[] args) {
        HashMap<Integer, String> family = new HashMap<Integer, String>();
         // 添加键值对
        family.put(1, "Grandpa");
        family.put(2, "Grandma");
        family.put(3, "Father");
        family.put(4, "Mother");
        family.put(5, "Children");
        family.remove(5);
        System.out.println(family);
    }
}

执行以上实例,输出结果为:

代码语言:javascript复制
{1=Grandpa, 2=Grandma, 3=Father, 4=Mother}

五、HashMap计算大小:

使用 对象名.size()方法

代码语言:javascript复制
import java.util.HashMap;

public class ChildrenTest {
public static void main(String[] args) {
        HashMap<Integer, String> family = new HashMap<Integer, String>();
         // 添加键值对
        family.put(1, "Grandpa");
        family.put(2, "Grandma");
        family.put(3, "Father");
        family.put(4, "Mother");
        family.put(5, "Children");
        System.out.println(family.size());
    }
}

执行以上实例,输出结果为:

代码语言:javascript复制
5

六、HashMap遍历元素:

使用 for-each遍历 HashMap 中的元素

代码语言:javascript复制
mport java.util.HashMap;

public class ChildrenTest {
public static void main(String[] args) {
        HashMap<Integer, String> family = new HashMap<Integer, String>();
         // 添加键值对
        family.put(1, "Grandpa");
        family.put(2, "Grandma");
        family.put(3, "Father");
        family.put(4, "Mother");
        family.put(5, "Children");
        // 输出 key 和 value
        for (Integer i : family .keySet()) {
            System.out.println("key: "   i   " value: "   family.get(i));
        }
         // 返回所有 value 值
        for(String value: family.values()) {
           // 输出每一个value
          System.out.print(value   ", ");
        }
    }
}

执行以上实例,输出结果为:

代码语言:javascript复制
key: 1 value: Grandpa
key: 2 value: Grandma
key: 3 value: Father
key: 4 value: Mother
key: 5 value: Children
Grandpa, Grandma, Father, Father,Children,

0 人点赞