011—JAVA中的Map<k,v>集合详解

本文深入探讨Java中Map接口的基本概念及其核心实现类HashMap的特点与使用方法,包括增删改查等基本操作,并通过示例代码展示如何遍历Map及处理自定义类型的键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Map<k,v>集合的实现类的家谱图

 Map:顶级接口 存储键值对

以HashMap实现类为例演示方法

---  HashMap
          key:无序   唯一
          value: 没有特殊要求 满足泛型即可

    注意:
        如果key重复了  新的value 会替换旧的value

Map中的常用方法

增  :   put()  添加键值对   putAll()

删:remove()  根据键删除值

改:replace(K,V)replace(K,V,V)

查:containsKey();  containsValue(); get()根据key返回value,没有返回null

见名知道意的方法:

                             isEmpty()    size()    clear()

其它: map.keySet()  (将所有的key存储到一个set集合中,因为key就是用set的存储的,保证了key的无序唯一)

             map.values()   获取所有的value

             entrySet()        获取所有的key--value集合

14个  

 

 @Test
    public void test03() {

        HashMap<String, Integer> hashMap = new HashMap<>();
        //判断集合是否为空
        System.out.println("hashMap.isEmpty() = " + hashMap.isEmpty());
        //集合内键值对的数量
        int size = hashMap.size();
        System.out.println("size = " + size);

        hashMap.put("安琪拉", 001);
        hashMap.put("嬴政", 002);
        System.out.println("hashMap.isEmpty() = " + hashMap.isEmpty());

        System.out.println("hashMap.size() = " + hashMap.size());

        System.out.println("--->"+hashMap);
        //清空集合
        hashMap.clear();

        System.out.println("hashMap.isEmpty() = " + hashMap.isEmpty());

        System.out.println(hashMap);

        Set<Map.Entry<String, Integer>> entries = hashMap.entrySet();


    }

    @Test
    public void test02() {

        HashMap<Integer, String> map = new HashMap<>();

        map.put(1, "张三");
        map.put(3, "李四");
        map.put(7, "王五");
        //获取所有的key
        Set<Integer> keys = map.keySet();

        for (Integer key : keys) {

            System.out.println("key = " + key);
        }

        //获取所有的value

        Collection<String> values = map.values();


        Iterator<String> iterator = values.iterator();

        while (iterator.hasNext()){

            String value = iterator.next();
            System.out.println("value = " + value);
        }

    }

    @Test
    public void test01() {
        //多态
        Map<String, String> map = new HashMap<>();

        //添加数据
        map.put("ch", "China");
        map.put("ja", "Japan");
        map.put("ru", "Russia");
        map.put("ru", "Russia1");
        map.put("ja", "HelloKity");
        System.out.println(map);//{ru=Russia1, ch=China, ja=HelloKity}
        //删除数据
        //根据key删除 键值对
        map.remove("ja");

        System.out.println(map);


        //判断指定的可以是否存在于集合内
        boolean containsKey = map.containsKey("ru");

        System.out.println("containsKey = " + containsKey);

        //判断指定的value 是否存在集合内
        boolean containsValue = map.containsValue("China");

        System.out.println("containsValue = " + containsValue);

        //根据key 获取value
        String value = map.get("ru");
        System.out.println("value = " + value);


    }

Map的遍历

public class BanLi {
    Map<String,String> map = new HashMap<>();
    @Before
    public void test00(){
        map.put("ch", "China");
        map.put("ja", "Japan");
        map.put("ru", "Russia");
        System.out.println("-------------数据添加-------------");

    }
    @Test
    public void test04(){

        Set<String> keySet = map.keySet();
        Iterator<String> iterator = keySet.iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            String value = map.get(key);
            System.out.println(key + "-------->" + value);
        }
    }
    @Test
    public void test03(){
        Set<Map.Entry<String, String>> entries = map.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> entry = iterator.next();
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "----->" +value);

        }


    }

    @Test
    public void test02(){
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "----->" + value);
        }

    }
    @Test
    public void test01(){

        Set<String> set = map.keySet();
        for (String key : set) {
            String value = map.get(key);
            System.out.println(key + "---->" + value);

        }
    }
}

Map的注意点

后续使用Map时
   key 内置的简单类型 String  Integer
但是 如果key 是自定义类型 Cat  Dog  Student
    HashMap
    LinkedHashMap  key 需要重写 hashCode equals() 保证无序唯一

    TreeMap  key 需要定义比较规则

public class MapTest2 {
    public static void main(String[] args) {
        TreeMap<Cat, Integer> treeMap = new TreeMap<>();
        Cat c1  = new Cat("小白", 3);
        Cat c2  = new Cat("小黑", 2);
        treeMap.put(c1, 10);
        treeMap.put(c2, 20);

        treeMap.size();
        System.out.println("treeMap = " + treeMap);


    }

}
class Cat implements Comparable{
    String name;
    int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Cat cat = (Cat) o;
        return age == cat.age &&
                Objects.equals(name, cat.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        Cat cat = (Cat)o;
        return this.age - cat.age;
    }
}

案例练习一

(1)从键盘输入本组学员的姓名和他的手机号码,
   存放到map中,姓名为key,手机号码为value,并且遍历显示

(2)再从键盘输入姓名,查询他的手机号码

HashMap<String, String> map = new HashMap<>();
        map.put("张三", "13699175106");
        map.put("李四", "13699175116");
        map.put("王五", "13699175126");
        map.put("赵六", "13699175136");

        Set<String> strings = map.keySet();
        for (String key : strings) {
            String value = map.get(key);
            System.out.println(key + "---->" + value);
        }
        System.out.println("请输入姓名:");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String s1 = map.get(s);
        System.out.println("s1 = " + s1);
Map<String,String> map = new HashMap<>();

        map.put("张三", "13699175106");
        map.put("李四", "13699175116");
        map.put("王五", "13699175126");
        map.put("赵六", "13699175136");

        //1.键盘输入对象
        Scanner in = new Scanner(System.in);
        //2.给出提示语句
        System.out.println("请您输入名字");
        //3:接收数据
        String name = in.nextLine();
        //4:通过key获取value
        String phone = map.get(name);
        if (phone!=null){
            System.out.println(name + "找到了电话为:" + phone);
        }else {
            System.out.println("没有这个人" + name );
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值