HashMap、创建并遍历HashMap集合、LinkedHashMap

本文介绍了HashMap,它基于哈希表实现,可快速根据键找值,是无序存储结构。还给出不同键值类型的HashMap创建及遍历案例,提到自定义对象作键时需重写方法。此外,介绍了LinkedHashMap,对比了HashMap和Hashtable的区别。

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

HashMap

HashMap是基于哈希表的Map实现。键是哈希表结构,可以保证键的唯一性。
HashMap 继承于AbstractMap,实现了Map接口。
HashMap的设计初衷主要是为了解决键值(key-value)对应的关联的,HashMap的优势是可以很快的根据键(key)找到该键对应的值(value)。注意一下,HashMap是一种无序的存储结构。

创建并遍历HashMap集合

案例1: 键String类型,值String类型

//HashMap<String, String>案例

import java.util.HashMap;
import java.util.Set;

public class HashMapDemo {
    public static void main(String[] args){
        //创建集合
        HashMap<String, String> hmap= new HashMap<String, String>();

        //添加元素
        hmap.put("杨绛","钱钟书");
        hmap.put("林徽因","徐志摩");
        hmap.put("陈红","陈凯歌");
        hmap.put("袁咏仪","张智霖");
        hmap.put("孙俪","邓超");

        //遍历
        Set<String> keys=hmap.keySet();
        for(String key: keys){
            String value=hmap.get(key);
            System.out.println(key+"----"+value);
        }
    }
}

案例2: 键Integer类型,值String类型

//HashMap<Integer,String>案例

import java.util.HashMap;
import java.util.Set;
public class HashMapDemo2 {
    public static void main(String[] args){
        HashMap<Integer,String> hmap= new HashMap<Integer, String>();

                //hmap.put(088,"zhang");//报错注意0开头是八进制,不能超过8
        hmap.put(22,"lin");
        hmap.put(11,"chen");
        hmap.put(66,"zhou");
        hmap.put(66,"zhu");
        hmap.put(33,"qi");

        Set<Integer> keys=hmap.keySet();
        for(Integer key: keys){
            String value= hmap.get(key);
            System.out.println(key+"---"+value);
        }
//        输出结果:
//            33---qi
//            66---zhu//两个66,值取的是后面的
//            22---lin
//            11---chen

    }

}

案例3:键String类型,值Student类型

//HashMap<String, Student>

import java.util.HashMap;
import java.util.Set;

public class HashMapDemo3 {
    public static void main(String[] args){
        HashMap<String, Student> hmap= new HashMap<String, Student>();

        hmap.put("2013003",new Student("刘雯",28));
        hmap.put("2013001",new Student("李健",40));
        hmap.put("2013022",new Student("晨晨",22));
        hmap.put("2013010",new Student("晨晨",22));

        Set<String> keys=hmap.keySet();
        for(String key: keys){
            Student value=hmap.get(key);
            System.out.println(key+"---"+value.getName()+"-"+value.getAge());
        }

    }
//    输出结果:
//            2013010---晨晨-22
//            2013003---刘雯-28
//            2013022---晨晨-22
//            2013001---李健-40
}
}

案例4:键Student类型(自定义类型),值String类型

注意:
这里就要想一下自定义对象Student作为键时,什么叫同一个键?
假定要求Student的成员变量值都相同,则为同一个对象。
所以要在Student类里面重写equals()方法和hashCode()方法。
上面的Integer类和String类本身都重写了equals()方法。

//HashMap<Student, String>
//Student作为键。要求Student的成员变量值都相同,则为同一个对象。重写了Student的equals和hashcode方法.

import java.util.HashMap;
import java.util.Set;

public class HashMapDemo4 {
    public static void main(String[] args){
        HashMap<Student,String> hmap= new HashMap<Student,String>();

        hmap.put(new Student("刘雯",28), "考研");
        hmap.put(new Student("李健",40), "保研");
        hmap.put(new Student("晨晨",22), "考研");
        hmap.put(new Student("晨晨",22), "保研");
        hmap.put(new Student("晨晨",26), "保研");

        Set<Student> keys= hmap.keySet();
        for(Student key: keys){
            String value= hmap.get(key);
            System.out.println(key.getName()+"-"+key.getAge()+"---"+value);
        }
    }
//    输出结果:
//    刘雯-28---考研
//    晨晨-26---保研
//    晨晨-22---保研
//    李健-40---保研
}

LinkedHashMap

基于哈希表和链表的Map接口实现。
具有可预知的迭代顺序。
由哈希表保证键的唯一。
由链表保证键的有序(注意:是存储和取出的顺序一致,而不是排序)。

//观察下面程序的运行结果

LinkedHashMap<String, String> lmap= new LinkedHashMap<String, String>();//无参构造,自然排序

        lmap.put("杨绛","钱钟书");
        lmap.put("林徽因","徐志摩");
        lmap.put("陈红","陈凯歌");
        lmap.put("袁咏仪","张智霖");
        lmap.put("孙俪","邓超");

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

//        输出结果:
//        杨绛--钱钟书
//        林徽因--徐志摩
//        陈红--陈凯歌
//        袁咏仪--张智霖
//        孙俪--邓超
//            可以看到取出和存储的循序一致(链表保证的)

HashMap、Hashtable区别

HashTable:线程安全,效率低。允许null键和null值。HashTable已经被淘汰了,不要在代码中再使用它。
HashMap:线程不安全,效率高。不允许null键和null值。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值