在本指南中,我们将通过实例学习LinkedHashMap
类对Map
接口的实现。
HashMap类并不保证元素的任何特定的迭代顺序。它并不跟踪元素的插入顺序,每次迭代时都以随机的顺序产生元素。
如果你想让Map
中的元素有一个可预测的迭代顺序,那么你可以使用LinkedHashMap
。
本指南涵盖了LinkedHashMap类所提供的所有重要API。
LinkedHashMap
类的概述LinkedHashMap
。put(String key, Integer value)
putIfAbsent(String key, Integer value)
LinkedHashMap
用于访问键和修改其相关值的APIisEmpty()
size()
containsKey(Object key)
containsValue(Object value)
get(Object key)
LinkedHashMap
删除API的例子remove(Object key)
remove(Object key, Object value)
LinkedHashMap
。LinkedHashMap
进行迭代。LinkedHashMap’s
条目集进行迭代。iterator().
对LinkedHashMap’s
条目集进行遍历。iterator()
和Java 8的forEachRemaining()
方法对LinkedHashMap’s
入口集进行迭代。LinkedHashMap
保持插入顺序示例LinkedHashMap
类概述Java LinkedHashMap
类是Map
接口的哈希表和关联列表实现,具有可预测的迭代顺序。它继承了HashMap
类并实现了Map
接口。
关于Java LinkedHashMap类的重要内容是。
LinkedHashMap
包含基于键的值。LinkedHashMap
中的元素的迭代顺序是可以预测的。HashMap
,LinkedHashMap
不是线程安全的。你必须在多线程环境中明确同步对LinkedHashMap的并发访问。LinkedHashMap
。下面的例子展示了如何创建一个LinkedHashMap
并向其添加新的键值对。
// Creating a LinkedHashMap
LinkedHashMap<String, Integer> dayNumberMapping = new LinkedHashMap<>();
// Adding new key-value pairs to the LinkedHashMap
dayNumberMapping.put("Mon", 1);
dayNumberMapping.put("Tus", 2);
dayNumberMapping.put("Wen", 3);
dayNumberMapping.put("Thu", 4);
dayNumberMapping.put("Fri", 5);
dayNumberMapping.put("Sat", 6);
// Add a new key-value pair only if the key does not exist
// in the LinkedHashMap, or is mapped to `null`
dayNumberMapping.putIfAbsent("Sun", 7);
System.out.println(dayNumberMapping);
输出:
{Mon=1, Tus=2, Wen=3, Thu=4, Fri=5, Sat=6, Sun=7}
put(String key, Integer value)
将指定的值与该Map中的指定键相关联(可选操作)。
// Creating a HashMap
LinkedHashMap<String, Integer> dayNumberMapping = new LinkedHashMap<>();
// Adding new key-value pairs to the LinkedHashMap
dayNumberMapping.put("Mon", 1);
dayNumberMapping.put("Tus", 2);
dayNumberMapping.put("Wen", 3);
putIfAbsent(String key, Integer value)
如果指定的键还没有与一个值关联(或者被映射为null),则将其与给定的值关联并返回null,否则返回当前值。
LinkedHashMap<String, Integer> dayNumberMapping = new LinkedHashMap<>();
dayNumberMapping.putIfAbsent("Sun", 7);
LinkedHashMap
用于访问键和修改其关联值的APILinkedHashMap
是否为空 |isEmpty()
LinkedHashMap
的大小|size()
LinkedHashMap
|containsKey()
中?LinkedHashMap
|containsValue()
中?LinkedHashMap
|get()
中获得与给定键相关的值?LinkedHashMap
|put()
中与给定键相关的值?isEmpty()
如果此Map不包含键值映射,则返回true。
Map<String, String> userCityMapping = new LinkedHashMap<>();
// Check if a HashMap is empty
System.out.println("is userCityMapping empty? : " + userCityMapping.isEmpty());
size()
返回该Map中键值映射的数量。如果该Map包含超过Integer.MAX_VALUE
的元素,返回Integer.MAX_VALUE.
。
Map<String, String> userCityMapping = new LinkedHashMap<>();
userCityMapping.put("John", "New York");
userCityMapping.put("Rajeev", "Bengaluru");
userCityMapping.put("Steve", "London");
// Find the size of a HashMap
System.out.println("We have the city information of " + userCityMapping.size() + " users");
containsKey(Object key)
如果这个Map包含指定键的映射,则返回真。更正式的说,如果且仅当这个Map包含一个键k的映射,且(key==null ? k==null: key.equals(k))时返回true。(最多只能有一个这样的映射)。)
Map<String, String> userCityMapping = new LinkedHashMap<>();
userCityMapping.put("John", "New York");
userCityMapping.put("Rajeev", "Bengaluru");
userCityMapping.put("Steve", "London");
String userName = "Steve";
// Check if a key exists in the HashMap
if(userCityMapping.containsKey(userName)) {
// Get the value assigned to a given key in the HashMap
String city = userCityMapping.get(userName);
System.out.println(userName + " lives in " + city);
} else {
System.out.println("City details not found for user " + userName);
}
containsValue(Object value)
如果该映射将一个或多个键映射到指定的值,则返回真。更正式地说,如果并且只在这个Map包含至少一个映射到一个值v,并且(value==null ? v==null : value.equals(v))时返回true。对于大多数Map
接口的实现来说,这个操作可能需要与Map大小成线性关系的时间。
Map<String, String> userCityMapping = new LinkedHashMap<>();
userCityMapping.put("John", "New York");
userCityMapping.put("Rajeev", "Bengaluru");
userCityMapping.put("Steve", "London");
// Check if a value exists in a HashMap
if(userCityMapping.containsValue("New York")) {
System.out.println("There is a user in the userCityMapping who lives in New York");
} else {
System.out.println("There is not user in the userCityMapping who lives in New York");
}
get(Object key)
返回指定的键所映射的值,如果这个Map不包含键的映射,则返回空值。
Map<String, String> userCityMapping = new LinkedHashMap<>();
userCityMapping.put("John", "New York");
userCityMapping.put("Rajeev", "Bengaluru");
userCityMapping.put("Steve", "London");
System.out.println("Lisa's city : " + userCityMapping.get("Steve"));
LinkedHashMap
删除API的例子LinkedHashMap
|remove(Object key)
中删除一个键。LinkedHashMap
中删除一个键,只有当它与一个给定的值相关联时才会删除 * remove(Object key, Object value)
remove(Object key)
如果这个Map中存在一个键的映射,那么就从这个Map中删除这个键的映射(可选操作)。更正式地说,如果这个Map包含一个从键k到值v的映射,并且(key==null ? k==null: key.equals(k)),这个映射就会被删除。(该Map最多只能包含一个这样的映射)。)
Map<String, String> husbandWifeMapping = new LinkedHashMap<>();
husbandWifeMapping.put("Jack", "Marie");
husbandWifeMapping.put("Chris", "Lisa");
husbandWifeMapping.put("Steve", "Jennifer");
String husband = "Chris";
String wife = husbandWifeMapping.remove(husband);
remove(Object key, Object value)
只有当指定的键目前被映射到指定的值时,才会删除该键的条目。
Map<String, String> husbandWifeMapping = new LinkedHashMap<>();
husbandWifeMapping.put("Jack", "Marie");
husbandWifeMapping.put("Chris", "Lisa");
husbandWifeMapping.put("Steve", "Jennifer");
// Remove a key from the HashMap only if it is mapped to the given value
// Ex - Divorce "Jack" only if He is married to "Linda"
boolean isRemoved = husbandWifeMapping.remove("Jack", "Linda");
System.out.println("Did Jack get removed from the mapping? : " + isRemoved);
###对LinkedHashMap
进行迭代
本节中的例子显示了对LinkedHashMap
进行迭代的各种方法。
LinkedHashMap
进行迭代。LinkedHashMap’s
条目集进行遍历。iterator().
对LinkedHashMap’s
entrySet进行遍历。iterator()
和Java 8的forEachRemaining()
方法对LinkedHashMap’s
entrySet进行迭代。forEach
和lambda表达式对LinkedHashMap
进行迭代。LinkedHashMap<String, String> userCityMapping = new LinkedHashMap<>();
userCityMapping.put("Rajeev", "Bengaluru");
userCityMapping.put("Chris", "London");
userCityMapping.put("David", "Paris");
userCityMapping.put("Jesse", "California");
userCityMapping.forEach((user, city) -> {
System.out.println(user + " => " + city);
});
LinkedHashMap's
entrySet进行迭代LinkedHashMap<String, String> userCityMapping = new LinkedHashMap<>();
userCityMapping.put("Rajeev", "Bengaluru");
userCityMapping.put("Chris", "London");
userCityMapping.put("David", "Paris");
userCityMapping.put("Jesse", "California");
userCityMapping.entrySet().forEach(entry -> {
System.out.println(entry.getKey() + " => " + entry.getValue());
});
iterator()
对LinkedHashMap
的entrySet进行迭代。LinkedHashMap<String, String> userCityMapping = new LinkedHashMap<>();
userCityMapping.put("Rajeev", "Bengaluru");
userCityMapping.put("Chris", "London");
userCityMapping.put("David", "Paris");
userCityMapping.put("Jesse", "California");
Iterator<Map.Entry<String, String>> userCityMappingIterator
= userCityMapping.entrySet().iterator();
while (userCityMappingIterator.hasNext()) {
Map.Entry<String, String> entry = userCityMappingIterator.next();
System.out.println(entry.getKey() + " => " + entry.getValue());
}
iterator()
和forEachRemaining()对LinkedHashMap
的入口集进行迭代。LinkedHashMap<String, String> userCityMapping = new LinkedHashMap<>();
userCityMapping.put("Rajeev", "Bengaluru");
userCityMapping.put("Chris", "London");
userCityMapping.put("David", "Paris");
userCityMapping.put("Jesse", "California");
userCityMappingIterator = userCityMapping.entrySet().iterator();
userCityMappingIterator.forEachRemaining(entry -> {
System.out.println(entry.getKey() + " => " + entry.getValue());
});
###LinkedHashMap
保持插入顺序的例子
public class MapInterfaceLinkedHashMapImpl {
public static void main(String[] args) {
linkedHashMap();
}
// Maintain insertion order.
private static void linkedHashMap() {
// Constructs an empty insertion-ordered LinkedHashMap instance with the
// default
// initial capacity (16) and load factor (0.75).
Map<String, String> linkedHahMap = new LinkedHashMap<>();
linkedHahMap.put("key1", "value1");
linkedHahMap.put("key3", "value3");
linkedHahMap.put("key2", "value2");
linkedHahMap.put("key0", "value0");
// loop linkedHahMap using java 8 forEach method
linkedHahMap.forEach((k, v) -> {
System.out.println(k);
System.out.println(v);
});
// loop linkedHahMap using before java 8 forEach method
for (Entry pair : linkedHahMap.entrySet()) {
System.out.println(pair.getKey());
System.out.println(pair.getValue());
}
}
}
输出。
key1
value1
key3
value3
key2
value2
key0
value0
key1
value1
key3
value3
key2
value2
key0
value0
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/06/guide-to-linkedhashmap-class.html
内容来源于网络,如有侵权,请联系作者删除!