Java中ArrayList和HashMap的区别
ArrayList是集合框架的一部分,存在于Java.util 包中。它为我们提供了Java中的动态数组。虽然,它可能比标准数组慢,但在需要对数组进行大量操作的程序中很有帮助。 HashMap是自Java 1.2 以来 Java 集合的一部分。它提供了Java Map 接口的基本实现。它将数据存储在(键,值)对中,为了访问一个值,必须知道它的键。 HashMap 之所以称为 HashMap,是因为它使用了一种称为 Hashing 的技术。
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 都允许为空。 ArrayList 允许空值和 HashMap 允许空键和值
- ArrayList 和 HashMap 都允许重复,ArrayList 允许重复元素,HashMap 允许重复值
- ArrayList 和 HashMap 都可以通过Java中的 Iterator 进行遍历。
- 两者 Somewhere 都使用数组,ArrayList 由数组支持,HashMap 也由 Array 内部实现
- 两者都使用get() 方法,ArrayList.get() 方法基于索引工作,而 HashMap.get() 方法采用对象类型的一个参数 key_element 并引用应该获取其关联值的键,因此两者都提供恒定时间性能。
到目前为止,我们从上面提供的媒体中获得了一些清晰的信息,现在我们将对它们执行一组操作,以便通过得出相同操作的输出差异来感知真正的差异,这增加了我们理解 ArrayList 和 HashMap 之间差异的智力。
- 层次结构与语法
- 维护广告订单
- 内存消耗
- 重复元素处理
- 轻松获取元素
- 空元素存储
Java中ArrayList和HashMap的区别
1.层次结构和语法
接口实现: ArrayList 实现 List 接口,而 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 不维护任何顺序,因此返回的键值对任何类型的顺序。
例子:
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 list = new ArrayList();
// 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 hm
= new HashMap();
// 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);
}
}
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 list = new ArrayList();
// 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 hm
= new HashMap();
// 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);
}
}
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 list = new ArrayList();
// 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 hm
= new HashMap();
// 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));
}
}
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 list = new ArrayList();
// 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 hm
= new HashMap();
// 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, B, C, D]
HashMap: {1=A, 2=B, 3=C, 4=D}
3.内存消耗
ArrayList 仅将元素存储为值,并在内部维护每个元素的索引。而 HashMap 存储具有键值对的元素,这意味着两个对象。所以HashMap比较占用内存。
语法:数组列表
list.add("A");
// String value is stored in ArrayList
语法:哈希映射
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 list = new ArrayList();
// 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 hm
= new HashMap();
// 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 中,元素是通过它们对应的键来获取的。这意味着必须始终记住密钥。
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 list = new ArrayList();
// 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 hm
= new HashMap();
// 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 list = new ArrayList();
// 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 hm
= new HashMap();
// 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 HashMapThe java ArrayList implements List Interface
The java HashMap implements Map interface
ArrayList always maintain the insertion order of the elements
HashMap does not maintain the insertion order. ArrayList only stores value or element
HashMap stores key and value pairs
ArrayList can contain duplicate elements
HashMap does not contain duplicate keys but contains duplicate values.
We can have any number of null elements in ArrayList
We can have only one null key and any number of null values in HashMap
ArrayList get() method always gives an O(1) performance
HashMap get()method can be O(1) in the best case and O(n) in the worst case