HashMap与LinkedHashMap (HashMap vs LinkedHashMap)
First, we will see how LinkedHashMap differs from HashMap in Java?
首先,我们将看到LinkedHashMap与Java中的HashMap有何不同?
Java中的LinkedHashMap (LinkedHashMap in Java)
This class is available in java.util package.
此类在java.util包中可用。
LinkedHashMap is a child class of HashMap.
LinkedHashMap是HashMap的子类。
LinkedHashMap is an implementation class of Map interface.
LinkedHashMap是Map接口的实现类。
The underlying data structure is a combination of Hashtable and LinkedList.
基础数据结构是Hashtable和LinkedList的组合。
In LinkedHashMap, "insertion order of the elements is preserved" that means the insertion order of the elements must be the same as the order of retrieving the elements.
在LinkedHashMap中 ,“保留元素的插入顺序”,这意味着元素的插入顺序必须与检索元素的顺序相同。
In LinkedHashMap, "duplicate values are allowed but keys are not allowed".
在LinkedHashMap中 ,“允许重复的值,但不允许键”。
This class is introduced in 1.4 version.
此类在1.4版本中引入。
We should go for LinkedHashMap where the insertion order of the element is important.
我们应该选择LinkedHashMap ,其中元素的插入顺序很重要。
Example:
例:
Let suppose we have a HashMap with few elements. Here we are adding the elements in the order is {Java=1000, C=2000, C++=3000, Ruby=4000, Python=1000,null=null, Django=null, null=10000} and if we are retrieving the elements so the order of retrieving elements can be different (i.e. it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like {Java=1000, C=2000, C++=3000, Ruby=4000, Python=1000, null=10000, Django=null}(i.e. order of insertion and retrieval will be same because insertion order is preserved).
假设我们有一个包含少量元素的HashMap。 在这里,我们按照{Java = 1000,C = 2000,C ++ = 3000,Ruby = 4000,Python = 1000,null = null,Django = null,null = 10000}的顺序添加元素,如果要检索元素因此,检索元素的顺序可以不同(即,不需要将元素的插入和检索顺序相同)。因此,输出将不同,并且顺序将类似于{Java = 1000,C = 2000,C ++ = 3000,Ruby = 4000,Python = 1000,null = 10000,Django = null}(即插入和检索的顺序相同,因为保留了插入顺序)。
// Java program to demonstrate the behavior of LinkedHashMap
import java.util.Collection;
import java.util.LinkedHashMap;
class LinkedHashMapClass {
public static void main(String[] args) {
// Creating an instance of LinkedHashMap
LinkedHashMap lhm = new LinkedHashMap();
//By using put() method to add some values in LinkedHashMap
lhm.put("Java", 1000);
lhm.put("C", 2000);
lhm.put("C++", 3000);
lhm.put("Ruby", 4000);
lhm.put("Python", 1000);
lhm.put("null", null);
lhm.put("Django", null);
/* Here one null will be accepted for keys */
lhm.put("null", 10000);
// Display retrieval order of HashMap
System.out.println("Current LinkedHashMap list is :" + lhm);
// by using values() to find values of HashMap
Collection values = lhm.values();
// Display Values of HashMap
System.out.println("Current LinkedHashMap Key values is :" + values);
}
}
Output
输出量
E:\Programs>javac LinkedHashMapClass.java
E:\Programs>java LinkedHashMapClass
Current LinkedHashMap list is :{Java=1000, C=2000, C++=3000, Ruby=4000,
Python=1000, null=10000, Django=null}
Current HashMap Key values is :[1000, 2000, 3000, 4000, 1000, 10000, null]
Second, we will see how HashMap differs from LinkedHashMap in Java?
其次,我们将看到HashMap与Java中的LinkedHashMap有何不同?
Java中的HashMap (HashMap in Java)
This class is also available in java.util package.
该类在java.util包中也可用。
HashMap is a parent class of LinkedHashMap.
HashMap是LinkedHashMap的父类。
HashMap is an implementation class of Map interface.
HashMap是Map接口的实现类。
The underlying data structure is a combination of Hashtable.
基础数据结构是哈希表的组合。
In HashMap, "insertion order of the elements is not preserved" that means the insertion order of the elements are not needed to be the same as the order of retrieving the elements.
在HashMap中 ,“不保留元素的插入顺序”,这意味着元素的插入顺序不需要与检索元素的顺序相同。
In HashMap, "duplicate values are allowed but keys are not allowed".
在HashMap中 ,“允许重复的值,但不允许键”。
This class is introduced in 1.2 version.
此类在1.2版本中引入。
We should go for HashMap where the insertion order of the element is not important.
我们应该使用HashMap ,其中元素的插入顺序并不重要。
Example:
例:
Let suppose we have a HashMap with few elements. Here we are adding the elements in the order is {Java=1000, C=2000, C++=3000, Ruby=4000, Python=1000,null=null, Django=null, null=7000} and if we are retrieving the elements so the order of retrieving elements can be different (i.e.insertion order is not preserved and it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like {Ruby=4000, C=2000, Django=null, Python=1000, C++=3000, null=7000, Java=1000}
假设我们有一个包含少量元素的HashMap。 在这里,我们按照{Java = 1000,C = 2000,C ++ = 3000,Ruby = 4000,Python = 1000,null = null,Django = null,null = 7000}的顺序添加元素,如果我们要检索元素因此检索元素的顺序可以不同(即插入顺序不会保留,也不需要与元素的插入和检索顺序相同。)因此输出将不同,顺序将类似于{Ruby = 4000, C = 2000,Django = null,Python = 1000,C ++ = 3000,null = 7000,Java = 1000}
// Java program to demonstrate the behavior of HashMap
import java.util.Collection;
import java.util.HashMap;
class HashMapClass {
public static void main(String[] args) {
// Creating an instance of HashMap
HashMap hm = new HashMap();
//By using put() method to add some values in HashMap
hm.put("Java", 1000);
hm.put("C", 2000);
hm.put("C++", 3000);
hm.put("Ruby", 4000);
hm.put("Python", 1000);
hm.put("null", null);
hm.put("Django", null);
// Here we will not get any error but one null is accepted for keys
hm.put("null", 7000);
// Display retrieval order of HashMap
System.out.println("Current HashMap list is :" + hm);
// by using values() to find values of HashMap
Collection values = hm.values();
// Display Values of HashMap
System.out.println("Current HashMap Key values is :" + values);
}
}
Output
输出量
E:\Programs>javac HashMapClass.java
E:\Programs>java HashMapClass
Current HashMap list is :{Ruby=4000, C=2000, Django=null, Python=1000,
C++=3000, null=7000, Java=1000}
Current HashMap Key values is :[4000, 2000, null, 1000, 3000, 7000, 1000]
翻译自: https://www.includehelp.com/java/differences-between-hashmap-and-linkedhashmap-in-java.aspx