ArrayList是的一部分采集框架并存在于java.util包。它为我们提供了Java中的动态数组。虽然它可能比标准数组慢,但对于需要对数组进行大量操作的程序很有帮助。HashMap自 Java 1.2 起就成为 Java 集合的一部分。它提供了基本的实现Map它将数据存储在(键,值)对中,为了访问值,必须知道它的键。 HashMap 也称为 HashMap,因为它使用了一种称为哈希的技术。
Hashing is a technique of converting a large String to a small String that represents the same String. A shorter value helps in indexing and faster searches. HashSet also uses HashMap internally. It internally uses a link list to store key-value pairs already explained in HashSet in detail and further articles.
在这里,我们将继续讨论它们之间的共同特征。然后,我们将通过在单个 Java 程序中对它们执行一组操作并感知输出之间的差异来讨论它们之间的差异。
首先,让我们讨论一下Java 中 ArrayList 和 HashMap 之间的相似之处
- ArrayList和HashMap,两者都不同步。所以为了在多线程环境中使用它们,需要首先进行同步。
- ArrayList 和 HashMap 都允许为 null。 ArrayList 允许空值,HashMap 允许空键和值
- ArrayList 和 HashMap 都允许重复,ArrayList 允许重复元素,HashMap 允许重复值
- ArrayList和HashMap都可以通过Java中的Iterator来遍历。
- 两者Somewhere都使用数组,ArrayList由数组支持,HashMap内部也由Array实现
- 两者都使用get()方法,ArrayList.get() 方法基于索引工作,而 HashMap.get() 方法采用一个对象类型的参数 key_element 表示应该获取其关联值的键,因此两者都提供恒定时间性能。

到目前为止,我们从上面提供的媒体中得到了一些清晰的信息,现在我们将对它们执行一组操作,以便通过同一操作的输出差异来感知真正的差异,这增加了我们理解 ArrayList 和 ArrayList 之间差异的智力。哈希映射。
- 层次结构和语法
- 维护广告订单
- 内存消耗
- 重复元素处理
- 轻松获取元素
- 空元素存储
Differences Between ArrayList and HashMap in Java
1. 层次结构和语法
接口实现:ArrayList 实现List Interface而 HashMap 是实现Map.
用法:ArrayList 类的声明
public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable
用法:HashMap 类的声明
public class HashMap extends AbstractMap implements Map, Cloneable, Serializable
2. 广告订单的维护
ArrayList 维护插入顺序,而 HashMap 不维护插入顺序,这意味着 ArrayList 以相同的顺序返回列表项,而 HashMap 不维护任何顺序,因此返回的 key-values 与任何类型的顺序配对。
例子:
Java
// Java Program to illustrate Maintenance of Insertion Order 
// in ArrayList vs HashMap  
  
// Importing all utility classes 
import java.util.*; 
  
// Main class 
class GFG { 
  
    // Main driver method 
    public static void main(String args[]) 
    { 
  
        // Creating ArrayList of string type 
        ArrayList<String> list = new ArrayList<String>(); 
  
        // Adding object in ArrayList 
        list.add("A"); 
        list.add("B"); 
        list.add("C"); 
        list.add("D"); 
  
        // Invoking ArrayList object 
        System.out.println("ArrayList: " + list); 
  
        // Creating HashMap 
        HashMap<Integer, String> hm 
            = new HashMap<Integer, String>(); 
  
        // Adding object in HashMap object created above 
        // using put() method 
        hm.put(1, "A"); 
        hm.put(2, "B"); 
        hm.put(3, "C"); 
        hm.put(4, "D"); 
  
        // Invoking HashMap object 
        // It might or might not display elements 
        // in the insertion order 
        System.out.print("Hash 
                         Map: " + hm); 
    } 
}ArrayList: [A, B, C, D]
HashMap: {1=A, 2=B, 3=C, 4=D}
3. 内存消耗
ArrayList 仅将元素存储为值,并在内部维护每个元素的索引。而 HashMap 存储带有键值对的元素,这意味着两个对象。所以HashMap相对来说占用更多的内存。
用法:ArrayList
list.add("A");
// String value is stored in ArrayList
用法:HashMap
hm.put(1, "A"); // Two String values stored // as the key value pair in HashMap
4. 重复元素处理
ArrayList 允许重复元素,而 HashMap 不允许重复键,但允许重复值。
示例
Java
// Java Program to Illustrate Duplicate Elements Insertion 
// in ArrayList vs HashMap 
  
// Importing utility classes 
import java.util.*; 
  
// Main class 
class GFG { 
  
    // Main driver method 
    public static void main(String args[]) 
    { 
        // Creating ArrayList of string type 
        ArrayList<String> list = new ArrayList<String>(); 
  
        // Adding object in ArrayList 
        list.add("A"); 
        list.add("B"); 
  
        // Add duplicates 
        list.add("A"); 
        list.add("A"); 
  
        // Invoking ArrayList object 
        System.out.println("ArrayList: " + list); 
  
        // Creating HashMap 
        HashMap<Integer, String> hm 
            = new HashMap<Integer, String>(); 
  
        // Adding object in HashMap 
        hm.put(1, "A"); 
        hm.put(2, "B"); 
  
        // Add duplicates key 
        // Change value if index exist 
        hm.put(3, "A"); 
        hm.put(3, "A"); 
  
        // Add duplicates values 
        // allow duplicates value 
        hm.put(4, "A"); 
        hm.put(5, "A"); 
  
        // Invoking HashMap object 
        System.out.print("HashMap: " + hm); 
    } 
}ArrayList: [A, B, A, A]
HashMap: {1=A, 2=B, 3=A, 4=A, 5=A}
5. 轻松获取元素
在ArrayList中,可以通过指定元素的索引来轻松获取元素。但在 HashMap 中,元素是通过对应的键来获取的。这意味着必须始终记住 key 。
Note: ArrayList get(index) method always gives O(1) time complexity While HashMap get(key) can be O(1) in the best case and O(n) in the worst case time complexity.
示例
Java
// Java Program to Illustrate Ease of fetching an Element 
// in ArrayList vs HashMap 
  
// Importing all utility classes 
import java.util.*; 
  
// Main class 
class GFG { 
  
    // main driver method 
    public static void main(String args[]) 
    { 
        // Creating ArrayList of string type 
        ArrayList<String> list = new ArrayList<String>(); 
  
        // Adding object in ArrayList 
        list.add("A"); 
        list.add("B"); 
        list.add("C"); 
        list.add("D"); 
  
        // Invoking ArrayList object 
        System.out.println("First Element of ArrayList: "
                           + list.get(0)); 
        System.out.println("Third Element of ArrayList: "
                           + list.get(2)); 
  
        // Creating HashMap 
        // Declaring object of integer and string type 
        HashMap<Integer, String> hm 
            = new HashMap<Integer, String>(); 
  
        // Adding object in HashMap 
        hm.put(1, "A"); 
        hm.put(2, "B"); 
        hm.put(3, "C"); 
        hm.put(4, "D"); 
  
        // Invoking HashMap object 
        System.out.println("HashMap value at Key 1: "
                           + hm.get(1)); 
        System.out.println("HashMap value at Key 3: "
                           + hm.get(3)); 
    } 
}First Element of ArrayList: A Third Element of ArrayList: C HashMap value at Key 1: A HashMap value at Key 3: C
6. 空元素存储
在ArrayList中,可以存储任意数量的空元素。而在HashMap中,只允许有一个空键,但值可以是任意数字。
示例
Java
// Java Program to Illustrate Null Element Storage in 
// Arraylist vs HashMap 
  
// Importing all utility classes 
import java.util.*; 
  
// Main class 
class GFG { 
  
    // main driver method 
    public static void main(String args[]) 
    { 
        // Creating ArrayList of string type 
        ArrayList<String> list = new ArrayList<String>(); 
  
        // Adding object in ArrayList 
        // using standard add() method 
        list.add("A"); 
  
        // Adding first null value 
        list.add(null); 
        list.add("C"); 
  
        // Adding two null value again 
        list.add(null); 
        list.add(null); 
  
        // Invoking ArrayList object 
        System.out.println("ArrayList: " + list); 
  
        // Creating HashMap 
        // Declaring object of integer and string type 
        HashMap<Integer, String> hm 
            = new HashMap<Integer, String>(); 
  
        // Adding object in HashMap 
        hm.put(1, "A"); 
        hm.put(2, "B"); 
  
        // add null key 
        hm.put(null, "C"); 
  
        // Again adding null key 
        // which replace value of first 
        // insert null key value 
        hm.put(null, null); 
  
        // Adding second null value 
        hm.put(3, null); 
  
        // Printing the elements of Hashmap 
        System.out.println("HashMap: " + hm); 
    } 
}ArrayList: [A, null, C, null, null]
HashMap: {null=null, 1=A, 2=B, 3=null}
那么,让我们弄清楚ArrayList 和 HashMap 之间的区别在表中:
| ArrayList | HashMap | 
|---|---|
| java ArrayList 实现列表接口 | java HashMap 实现Map接口 | 
| ArrayList始终保持元素的插入顺序 | HashMap 不维护插入顺序。 | 
| ArrayList仅存储值或元素 | HashMap 存储键值对 | 
| ArrayList 可以包含重复元素 | HashMap 不包含重复的键,但包含重复的值。 | 
| ArrayList 中可以有任意数量的 null 元素 | HashMap 中只能有一个空键和任意数量的空值 | 
| ArrayList get() 方法始终提供 O(1) 性能 | HashMap get()method 在最好的情况下可以是 O(1),在最坏的情况下可以是 O(n) | 
相关用法
- Java ArrayList和HashSet的区别用法及代码示例
- Java ArrayList add()用法及代码示例
- Java ArrayList addAll()用法及代码示例
- Java ArrayList clear()用法及代码示例
- Java ArrayList clone()用法及代码示例
- Java ArrayList contains()用法及代码示例
- Java ArrayList get()用法及代码示例
- Java ArrayList indexOf()用法及代码示例
- Java ArrayList removeAll()用法及代码示例
- Java ArrayList remove()用法及代码示例
- Java ArrayList size()用法及代码示例
- Java ArrayList isEmpty()用法及代码示例
- Java ArrayList subList()用法及代码示例
- Java ArrayList set()用法及代码示例
- Java ArrayList sort()用法及代码示例
- Java ArrayList toArray()用法及代码示例
- Java ArrayList toString()用法及代码示例
- Java ArrayList ensureCapacity()用法及代码示例
- Java ArrayList lastIndexOf()用法及代码示例
- Java ArrayList retainAll()用法及代码示例
- Java ArrayList containsAll()用法及代码示例
- Java ArrayList trimToSize()用法及代码示例
- Java ArrayList removeRange()用法及代码示例
- Java ArrayList replaceAll()用法及代码示例
- Java ArrayList removeIf()用法及代码示例
注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Difference Between ArrayList and HashMap in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
