Java中Map接口实现类的工作原理如何解析?

2026-05-26 05:391阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1759个文字,预计阅读时间需要8分钟。

Java中Map接口实现类的工作原理如何解析?

%E2%80%9CMap%E6%8E%A5%E5%8F%A3%E6%8F%90%E4%BE%9B%E4%BA%86%E4%B8%80%E7%A7%8D%E6%98%A0%E5%B0%84%E5%85%B3%E7%B3%BB%EF%BC%8C%E5%85%B6%E4%B8%AD%E7%9A%84%E5%85%83%E7%B4%A0%E6%98%AF%E4%BB%A5%E9%94%AE%E5%80%BC%E5%AF%B9%EF%BC%88key-value%E2%80%89%E7%9A%84%E5%BD%A2%E5%BC%8F%E5%AD%98%E5%82%A8%E7%9A%84%EF%BC%8C%E8%83%BD%E5%A4%9F%E5%AE%9E%E7%8E%B0%E6%A0%B9%E6%8D%AEkey%E5%BF%AB%E9%80%9F%E6%9F%A5%E6%89%BEvalue%EF%BC%9B%E5%85%B6%E4%B8%AD%E7%9A%84%E9%94%AE%E5%80%BC%E5%AF%B9%E4%BB%A5Entry%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%AF%B9%E8%B1%A1%E5%AE%9E%E4%BE%8B%E5%BD%A2%E5%BC%8F%E5%AD%98%E5%9C%A8%EF%BC%8C%E5%BB%BA%E2%80%9Ckey%E5%80%BC%E2%80%9D%E4%B8%8D%E8%A6%81%E9%87%8D%E5%A4%8D%E2%80%9D%E2%80%9D

Map接口

Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value;

Map中的键值对以Entry类型的对象实例形式存在;
建(key值)不可重复,value值可以重复,一个value值可以和很多key值形成对应关系,每个建最多只能映射到一个值。

Map支持泛型,形式如:Map<K,V>

Map中使用put(K key,V value)方法添加

Map接口中定义的常用方法

具体使用在实现类中讨论

int size();//获取Map集合大小(即元素数量) boolean isEmpty();//判断是否为空 boolean containsKey(Object key);//判断是否包含某个键 boolean containsValue(Object value);//判断是否包含某个值 V get(Object key);//获取某个键对应的值 V put(K key, V value);//添加键值对(K,V) V remove(Object key);//移除某个键对应的键值对 void putAll(Map<? extends K, ? extends V> m);//添加另一个Map集合 void clear();//清空所有键值对 Set<K> keySet();//获取键的集合 Collection<V> values();//获取值的集合 Set<Map.Entry<K, V>> entrySet();//获取键值对实体的集合 interface Entry<K,V>//Map中的内部接口

HashMap

基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)除实现了Map接口外还实现了Cloneable,Serializable,继承了AbstractMap抽象类

此类不保证映射的顺序,特别是它不保证该顺序恒久不变。

特点:

  • 键无序,唯一,类似于Set集合
  • 值有序,可重复,类似于List
  • 底层数据结构是哈希表,保证键唯一

允许键为null,值为null

// HashMap<String, Student> hm = new HashMap<String, Student>(); // hm.put("2018050401", new Student("2018050401", "张三", 18, 80.0)); // hm.put("2018050402", new Student("2018050402", "李四", 18, 80.0)); // hm.put("2018050403", new Student("2018050403", "李四", 18, 80.0)); // hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0)); // hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0)); // // // 方式一: 通过键找值 // Set<String> keys = hm.keySet(); // for (String key : keys) { // Student s = hm.get(key); // System.out.println(key + "|" + s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore()); // } HashMap<Student, String> hm = new HashMap<Student, String>(); hm.put(new Student("2018050401", "张三", 18, 80.0),"2018050401"); hm.put(new Student("2018050402", "李四", 18, 80.0),"2018050402"); hm.put(new Student("2018050403", "李四", 18, 80.0), "2018050403"); hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404"); hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404"); // 方式二: 通过键值对对象找键找值 Set<Entry<Student, String>> keyValues = hm.entrySet(); for (Entry<Student, String> keyValue : keyValues) { Student s = keyValue.getKey(); String value = keyValue.getValue(); System.out.println(s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore() + "=" + value); }

LinkedHashMap

Map 接口的哈希表和链表实现,具有可预知的迭代顺序

特点:

  • 键有序,唯一,
  • 值有序,可重复,类似于List

底层数据结构是哈希表和链表,哈希表保证键唯一,链表保证键有序

LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>(); lhm.put(01, "张三1"); lhm.put(02, "张三2"); lhm.put(03, "张三3"); lhm.put(04, "张三4"); lhm.put(05, "张三5"); Set<Integer> keys = lhm.keySet(); for (Integer key : keys) { System.out.println(key + "|" + lhm.get(key)); }

TreeMap

基于红黑树(Red-Black tree)的 NavigableMap 实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,

具体取决于使用的构造方法。

特点:

  • 键可排序,唯一,
  • 值有序,可重复,类似于List
  • 底层数据结构是自平衡的二叉树,可排序

排序方式类似于TreeSet,分为自然排序和比较器排序,具体取决于使用的构造方法

TreeMap<Integer, String> tm = new TreeMap<Integer, String>(); tm.put(24, "Hello1"); tm.put(14, "Hello2"); tm.put(34, "Hello3"); tm.put(124, "Hello4"); tm.put(24, "Hello5"); tm.put(24, "Hello6"); tm.put(24, "Hello7"); tm.put(244, "Hello8"); tm.put(624, "Hello9"); tm.put(24, "Hello10"); Set<Integer> keys = tm.keySet(); for (Integer key : keys) { String value = tm.get(key); System.out.println(key + "|" + value); }

HashTable

此类实现一个哈希表,该哈希表将键映射到相应的值。任何非 null 对象都可以用作键或值

特点:

  • 不允许null键和null值
  • 线程安全,效率低

HashMap和Hashtable的区别:

  • HashMap是不安全的不同步的效率高的 允许null键和null值
  • Hashtable是安全的同步的效率低的 不允许null键和null值

底层都是哈希表结构

Java中Map接口实现类的工作原理如何解析?

Hashtable<String, String> hashtable = new Hashtable<String, String>(); hashtable.put("刘备", "孙尚香"); hashtable.put("孙策", "大乔"); hashtable.put("周瑜", "小乔"); hashtable.put("吕布", "貂蝉"); System.out.println(hashtable); Enumeration<String> keys = hashtable.keys(); while (keys.hasMoreElements()) { String key = keys.nextElement(); String value = hashtable.get(key); System.out.println(key + "|" + value); }

WeakHashMap

以弱键 实现的基于哈希表的 Map。在 WeakHashMap 中,当某个键不再正常使用时,将自动移除其条目。更精确地说,对于一个给定的键,其映射的存在并不阻止垃圾回收器对该键的丢弃,这就使该键成为可终止的,被终止,然后被回收。
丢弃某个键时,其条目从映射中有效地移除,因此,该类的行为与其他的 Map 实现有所不同。

WeakHashMap<String,String> whm = new WeakHashMap<>(); whm.put(new String("hello1"), "world1"); whm.put(new String("hello2"), "world2"); whm.put(new String("hello3"), "world3"); whm.put("hello4", "world3"); System.out.println(whm); System.gc(); System.runFinalization(); System.out.println(whm);

键是枚举类型

EnumMap<Direction, String> em = new EnumMap<>(Direction.class); em.put(Direction.UP, "向上移动"); em.put(Direction.DOWN, "向下移动"); em.put(Direction.LEFT, "向左移动"); em.put(Direction.RIGHT, "向右移动"); Set<Direction> keys = em.keySet(); for (Direction key : keys) { String value = em.get(key); System.out.println(key + "|" + value); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计1759个文字,预计阅读时间需要8分钟。

Java中Map接口实现类的工作原理如何解析?

%E2%80%9CMap%E6%8E%A5%E5%8F%A3%E6%8F%90%E4%BE%9B%E4%BA%86%E4%B8%80%E7%A7%8D%E6%98%A0%E5%B0%84%E5%85%B3%E7%B3%BB%EF%BC%8C%E5%85%B6%E4%B8%AD%E7%9A%84%E5%85%83%E7%B4%A0%E6%98%AF%E4%BB%A5%E9%94%AE%E5%80%BC%E5%AF%B9%EF%BC%88key-value%E2%80%89%E7%9A%84%E5%BD%A2%E5%BC%8F%E5%AD%98%E5%82%A8%E7%9A%84%EF%BC%8C%E8%83%BD%E5%A4%9F%E5%AE%9E%E7%8E%B0%E6%A0%B9%E6%8D%AEkey%E5%BF%AB%E9%80%9F%E6%9F%A5%E6%89%BEvalue%EF%BC%9B%E5%85%B6%E4%B8%AD%E7%9A%84%E9%94%AE%E5%80%BC%E5%AF%B9%E4%BB%A5Entry%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%AF%B9%E8%B1%A1%E5%AE%9E%E4%BE%8B%E5%BD%A2%E5%BC%8F%E5%AD%98%E5%9C%A8%EF%BC%8C%E5%BB%BA%E2%80%9Ckey%E5%80%BC%E2%80%9D%E4%B8%8D%E8%A6%81%E9%87%8D%E5%A4%8D%E2%80%9D%E2%80%9D

Map接口

Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value;

Map中的键值对以Entry类型的对象实例形式存在;
建(key值)不可重复,value值可以重复,一个value值可以和很多key值形成对应关系,每个建最多只能映射到一个值。

Map支持泛型,形式如:Map<K,V>

Map中使用put(K key,V value)方法添加

Map接口中定义的常用方法

具体使用在实现类中讨论

int size();//获取Map集合大小(即元素数量) boolean isEmpty();//判断是否为空 boolean containsKey(Object key);//判断是否包含某个键 boolean containsValue(Object value);//判断是否包含某个值 V get(Object key);//获取某个键对应的值 V put(K key, V value);//添加键值对(K,V) V remove(Object key);//移除某个键对应的键值对 void putAll(Map<? extends K, ? extends V> m);//添加另一个Map集合 void clear();//清空所有键值对 Set<K> keySet();//获取键的集合 Collection<V> values();//获取值的集合 Set<Map.Entry<K, V>> entrySet();//获取键值对实体的集合 interface Entry<K,V>//Map中的内部接口

HashMap

基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)除实现了Map接口外还实现了Cloneable,Serializable,继承了AbstractMap抽象类

此类不保证映射的顺序,特别是它不保证该顺序恒久不变。

特点:

  • 键无序,唯一,类似于Set集合
  • 值有序,可重复,类似于List
  • 底层数据结构是哈希表,保证键唯一

允许键为null,值为null

// HashMap<String, Student> hm = new HashMap<String, Student>(); // hm.put("2018050401", new Student("2018050401", "张三", 18, 80.0)); // hm.put("2018050402", new Student("2018050402", "李四", 18, 80.0)); // hm.put("2018050403", new Student("2018050403", "李四", 18, 80.0)); // hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0)); // hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0)); // // // 方式一: 通过键找值 // Set<String> keys = hm.keySet(); // for (String key : keys) { // Student s = hm.get(key); // System.out.println(key + "|" + s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore()); // } HashMap<Student, String> hm = new HashMap<Student, String>(); hm.put(new Student("2018050401", "张三", 18, 80.0),"2018050401"); hm.put(new Student("2018050402", "李四", 18, 80.0),"2018050402"); hm.put(new Student("2018050403", "李四", 18, 80.0), "2018050403"); hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404"); hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404"); // 方式二: 通过键值对对象找键找值 Set<Entry<Student, String>> keyValues = hm.entrySet(); for (Entry<Student, String> keyValue : keyValues) { Student s = keyValue.getKey(); String value = keyValue.getValue(); System.out.println(s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore() + "=" + value); }

LinkedHashMap

Map 接口的哈希表和链表实现,具有可预知的迭代顺序

特点:

  • 键有序,唯一,
  • 值有序,可重复,类似于List

底层数据结构是哈希表和链表,哈希表保证键唯一,链表保证键有序

LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>(); lhm.put(01, "张三1"); lhm.put(02, "张三2"); lhm.put(03, "张三3"); lhm.put(04, "张三4"); lhm.put(05, "张三5"); Set<Integer> keys = lhm.keySet(); for (Integer key : keys) { System.out.println(key + "|" + lhm.get(key)); }

TreeMap

基于红黑树(Red-Black tree)的 NavigableMap 实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,

具体取决于使用的构造方法。

特点:

  • 键可排序,唯一,
  • 值有序,可重复,类似于List
  • 底层数据结构是自平衡的二叉树,可排序

排序方式类似于TreeSet,分为自然排序和比较器排序,具体取决于使用的构造方法

TreeMap<Integer, String> tm = new TreeMap<Integer, String>(); tm.put(24, "Hello1"); tm.put(14, "Hello2"); tm.put(34, "Hello3"); tm.put(124, "Hello4"); tm.put(24, "Hello5"); tm.put(24, "Hello6"); tm.put(24, "Hello7"); tm.put(244, "Hello8"); tm.put(624, "Hello9"); tm.put(24, "Hello10"); Set<Integer> keys = tm.keySet(); for (Integer key : keys) { String value = tm.get(key); System.out.println(key + "|" + value); }

HashTable

此类实现一个哈希表,该哈希表将键映射到相应的值。任何非 null 对象都可以用作键或值

特点:

  • 不允许null键和null值
  • 线程安全,效率低

HashMap和Hashtable的区别:

  • HashMap是不安全的不同步的效率高的 允许null键和null值
  • Hashtable是安全的同步的效率低的 不允许null键和null值

底层都是哈希表结构

Java中Map接口实现类的工作原理如何解析?

Hashtable<String, String> hashtable = new Hashtable<String, String>(); hashtable.put("刘备", "孙尚香"); hashtable.put("孙策", "大乔"); hashtable.put("周瑜", "小乔"); hashtable.put("吕布", "貂蝉"); System.out.println(hashtable); Enumeration<String> keys = hashtable.keys(); while (keys.hasMoreElements()) { String key = keys.nextElement(); String value = hashtable.get(key); System.out.println(key + "|" + value); }

WeakHashMap

以弱键 实现的基于哈希表的 Map。在 WeakHashMap 中,当某个键不再正常使用时,将自动移除其条目。更精确地说,对于一个给定的键,其映射的存在并不阻止垃圾回收器对该键的丢弃,这就使该键成为可终止的,被终止,然后被回收。
丢弃某个键时,其条目从映射中有效地移除,因此,该类的行为与其他的 Map 实现有所不同。

WeakHashMap<String,String> whm = new WeakHashMap<>(); whm.put(new String("hello1"), "world1"); whm.put(new String("hello2"), "world2"); whm.put(new String("hello3"), "world3"); whm.put("hello4", "world3"); System.out.println(whm); System.gc(); System.runFinalization(); System.out.println(whm);

键是枚举类型

EnumMap<Direction, String> em = new EnumMap<>(Direction.class); em.put(Direction.UP, "向上移动"); em.put(Direction.DOWN, "向下移动"); em.put(Direction.LEFT, "向左移动"); em.put(Direction.RIGHT, "向右移动"); Set<Direction> keys = em.keySet(); for (Direction key : keys) { String value = em.get(key); System.out.println(key + "|" + value); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。