Java LinkedHashMap类

Java中的LinkedHashMap (LinkedHashMap in Java)

  1. It’s part of Java Collections Framework.

    它是Java Collections Framework的一部分。
  2. LinkedHashMap is the hash table and linked list implementation of the Map interface.

    LinkedHashMap是Map接口的哈希表和链接列表实现。
  3. LinkedHashMap extends HashMap class.

    LinkedHashMap扩展了HashMap类。
  4. LinkedHashMap maintains the order of insertion. So while iterating over its keys, the elements are returned in the order they were inserted.

    LinkedHashMap保持插入顺序。 因此,在遍历其键时,将按插入顺序返回元素。
  5. LinkedHashMap uses a doubly-linked list to maintain the order of insertion.

    LinkedHashMap使用双向链接列表来维护插入顺序。
  6. If a key is reinserted, its insertion order is not affected.

    如果重新插入密钥,则其插入顺序不会受到影响。
  7. It’s useful when you want a Map where we can iterate over the entries in the order of insertion but don’t want to deal with the additional overhead associated with the TreeMap.

    当您想要一个Map时,我们可以按插入顺序遍历条目,但又不想处理与TreeMap相关的额外开销时,这很有用。
  8. LinkedHashMap allows null entries for key and value. There can be a single null key and multiple null values.

    LinkedHashMap允许键和值的空条目。 可以有一个null键和多个null值。
  9. LinkedHashMap is not thread-safe. If you want to use it in a multi-threaded environment, you can create a synchronized wrapper over it using the Collections.synchronizedMap() method.

    LinkedHashMap不是线程安全的。 如果要在多线程环境中使用它,则可以使用Collections.synchronizedMap()方法在其上创建一个同步包装器。

LinkedHashMap构造函数 (LinkedHashMap Constructors)

There are 5 constructors in LinkedHashMap class.

LinkedHashMap类中有5个构造函数。

LinkedHashMap Constructors

LinkedHashMap Constructors

LinkedHashMap构造函数

  1. LinkedHashMap(): creates an empty LinkedHashMap instance with the default initial capacity (16) and load factor (0.75).

    LinkedHashMap() :创建一个空的LinkedHashMap实例,其默认初始容量(16)和负载系数(0.75)。
  2. LinkedHashMap(int initialCapacity): creates an empty LinkedHashMap instance with the given initial capacity and load factor (0.75). It throws IllegalArgumentException if the initialCapacity is negative.

    LinkedHashMap(int initialCapacity) :使用给定的初始容量和负载因子(0.75)创建一个空的LinkedHashMap实例。 如果initialCapacity为负,则抛出IllegalArgumentException
  3. LinkedHashMap(int initialCapacity, float loadFactor): creates an empty LinkedHashMap instance with the given initial capacity and load factor. It throws IllegalArgumentException if the initial capacity is negative or the load factor is nonpositive.

    LinkedHashMap(int initialCapacity,float loadFactor) :使用给定的初始容量和负载因子创建一个空的LinkedHashMap实例。 如果初始容量为负或负载系数为正,则抛出IllegalArgumentException。
  4. LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder): creates an empty LinkedHashMap with the given initial capacity, load factor, and ordering mode. If accessOrder is true – the ordering mode is access-order, otherwise insertion-order.

    LinkedHashMap(int initialCapacity,float loadFactor,boolean accessOrder) :使用给定的初始容量,负载因子和订购方式创建一个空的LinkedHashMap。 如果accessOrder为true –排序模式为access-order,否则为insert-order。
  5. LinkedHashMap(Map<? extends K, ? extends V> m): creates an insertion-ordered LinkedHashMap instance with the same mappings as the specified map. It throws NullPointerException if the given map is null.

    LinkedHashMap(Map <?扩展K,?扩展V> m) :创建一个插入顺序的LinkedHashMap实例,该实例具有与指定映射相同的映射。 如果给定的映射为null,则抛出NullPointerException

Here is a simple example to confirm that the LinkedHashMap maintains the order of insertion for its keys.

这是一个简单的示例,用于确认LinkedHashMap维护其键的插入顺序。

package com.journaldev.examples;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class LinkedHashMapExamples {

	public static void main(String[] args) {

		Map<Integer, String> vowelsMap = new HashMap<>();
		vowelsMap.put(1, "a");
		vowelsMap.put(2, "e");
		vowelsMap.put(5, "u");
		vowelsMap.put(4, "o");
		vowelsMap.put(3, "i");

		vowelsMap.forEach((k, v) -> System.out.println("[" + k + "," + v + "]"));

		LinkedHashMap<Integer, String> lhm = new LinkedHashMap<>();
		lhm.put(1, "A");
		lhm.put(2, "E");
		lhm.put(5, "U");
		lhm.put(4, "O");
		lhm.put(3, "I");

		lhm.forEach((k, v) -> System.out.println("[" + k + "," + v + "]"));
	}
}

Output:

输出:

[1,a]
[2,e]
[3,i]
[4,o]
[5,u]

[1,A]
[2,E]
[5,U]
[4,O]
[3,I]

LinkedHashMap方法 (LinkedHashMap Methods)

There are no exclusive methods for LinkedHashMap class. It utilizes the methods from the HashMap class by extending it. But, some of the methods are overridden for the insertion order of entries.

LinkedHashMap类没有专用方法。 它通过扩展使用HashMap类中的方法。 但是,某些方法对于条目的插入顺序被覆盖。

  • containsValue(Object value): utilizes the LinkedList approach for faster resolution.

    containsValue(Object value) :利用LinkedList方法获得更快的分辨率。
  • get(Object key), getOrDefault(Object key, V defaultValue)

    get(对象键),getOrDefault(对象键,V defaultValue)
  • clear()

    明确()
  • keySet(): an instance of LinkedKeySet is returned.

    keySet():返回LinkedKeySet的实例。
  • forEach(), replaceAll(): utilizes the linked list implementation approach for insertion-order iteration and faster processing.

    forEach(),replaceAll():利用链表实现方法进行插入顺序迭代和更快的处理。

LinkedHashMap和HashMap (LinkedHashMap vs HashMap)

  • LinkedHashMap extends HashMap class. So HashMap is the superclass of LinkedHashMap class.

    LinkedHashMap扩展了HashMap类。 所以HashMap是LinkedHashMap类的超类。
  • LinkedHashMap maintains the order of insertion where HashMap doesn’t maintain any ordering of entries.

    LinkedHashMap保持插入顺序,而HashMap则不保持条目的任何顺序。
  • Iterating over the LinkedHashMap entries always produce the same sequence of elements. Iterating over the HashMap entries can produce random sequence because the order is not maintained.

    在LinkedHashMap条目上进行迭代始终会产生相同的元素序列。 遍历HashMap条目可能会产生随机序列,因为不维护顺序。
  • LinkedHashMap performance is slightly below that of HashMap, due to the added expense of maintaining the linked list, with one exception: Iteration over the collection-views of a LinkedHashMap requires time proportional to the size of the map, regardless of its capacity. Iteration over a HashMap is likely to be more expensive, requiring time proportional to its capacity.

    LinkedHashMap的性能比维护HashMap的性能略低,这归因于维护链表的额外费用,但有一个例外:对LinkedHashMap的集合视图进行迭代需要的时间与地图的大小成正比,而不论其容量如何。 在HashMap上进行迭代可能会更昂贵,需要的时间与其容量成正比。

LinkedHashMap和TreeMap (LinkedHashMap vs TreeMap)

If you want to access Map elements in the insertion-order, LinkedHashMap is recommended. It comes with fast performance and without incurring the increased cost associated with TreeMap.

如果要按插入顺序访问Map元素,建议使用LinkedHashMap。 它具有快速的性能,并且不会增加与TreeMap相关的成本。

Reference: LinkedHashMap API Docs

参考LinkedHashMap API文档

翻译自: https://www.journaldev.com/33438/java-linkedhashmap-class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值