Java中HashMap和LinkedHashMap之间的区别

HashMap与LinkedHashMap

首先,我们将看到LinkedHashMap与Java中的HashMap有何不同?

Java中的LinkedHashMap

  • 此类在java.util包中可用。

  • LinkedHashMapHashMap的子类。

  • LinkedHashMap是Map接口的实现类。

  • 基础数据结构是Hashtable和LinkedList的组合。

  • LinkedHashMap中,“保留元素的插入顺序”,这意味着元素的插入顺序必须与检索元素的顺序相同。

  • LinkedHashMap中,“允许重复的值,但不允许键”。

  • 此类在1.4版本中引入。

  • 我们应该使用LinkedHashMap,其中元素的插入顺序很重要。

示例

假设我们有一个包含少量元素的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程序演示LinkedHashMap的行为
import java.util.Collection;
import java.util.LinkedHashMap;

class LinkedHashMapClass {
    public static void main(String[] args) {
        //创建一个LinkedHashMap的实例
        LinkedHashMap lhm = new LinkedHashMap();

        //通过使用put()方法在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);

        //显示HashMap的检索顺序
        System.out.println("Current LinkedHashMap list is :" + lhm);

        //通过使用values()查找HashMap的值
        Collection values = lhm.values();

        //显示HashMap的值
        System.out.println("Current LinkedHashMap Key values is :" + values);
    }
}

输出结果

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]

其次,我们将看到HashMap与Java中的LinkedHashMap有何不同?

Java中的HashMap

  • 该类在java.util包中也可用。

  • HashMapLinkedHashMap的父类。

  • HashMap是Map接口的实现类。

  • 基础数据结构是哈希表的组合。

  • HashMap中,“不保留元素的插入顺序”,这意味着元素的插入顺序不需要与检索元素的顺序相同。

  • HashMap中,“允许重复值,但不允许键”。

  • 此类在1.2版本中引入。

  • 我们应该使用HashMap,其中元素的插入顺序并不重要。

示例

假设我们有一个包含少量元素的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程序演示HashMap的行为
import java.util.Collection;
import java.util.HashMap;

class HashMapClass {
    public static void main(String[] args) {
        //创建一个HashMap的实例
        HashMap hm = new HashMap();

        //通过使用put()方法在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);

        //这里我们不会得到任何错误,但是键接受一个null-
        hm.put("null", 7000);

        //显示HashMap的检索顺序
        System.out.println("Current HashMap list is :" + hm);

        //通过使用values()查找HashMap的值
        Collection values = hm.values();

        //显示HashMap的值
        System.out.println("Current HashMap Key values is :" + values);
    }
}

输出结果

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]