So sánh HashMap và Hashtable trong Java

Trong các bài viết trước, tôi đã giới thiệu với các bạn HashMap và HashTable trong Java. Trong bài này, chúng ta cùng so sánh sự giống nhau và khác nhau giữa chúng.

1. Giống nhau của HashMap và Hashtable

  • Cả HashMap và Hashtable đều cài đặt interface Map.
  • HashMap và Hashtable đều được sử dụng để lưu trữ dữ liệu ở dạng cặp key và value.
  • Cả hai đều đang sử dụng kỹ thuật băm để lưu trữ các khóa duy nhất.

2. Khác nhau của HashMap và Hashtable

HashMapHashtable
HashMap cho phép một key là null và nhiều giá trị null.Hashtable không cho phép bất kỳ key hoặc giá trị null.
HashMap không đồng bộ.Hashtable là đồng bộ.
HashMap nhanh hơn Hashtable .Hashtable chậm hơn HashMap.
HashMap được duyệt bởi Iterator.Hashtable được duyệt bởi Enumerator và Iterator.
Iterator trong HashMap là fail-fast.Enumerator trong Hashtable là không fail-fast.
HashMap kế thừa lớp AbstractMap.Hashtable kế thừa lớp Dictionary.
Chúng ta có thể làm cho HashMap đồng bộ bằng cách gọi phương thức:
Map m = Collections.synchronizedMap(hashMap);
Hashtable được đồng bộ nội bộ và không thể hủy đồng bộ hóa.
HashMap được ưa thích trong các ứng dụng đơn luồng (single-thread). Nếu bạn muốn sử dụng HashMap trong ứng dụng đa luồng (mulit-thread), có thể thực hiện bằng cách sử dụng phương thức Collections.synchronizedMap().Mặc dù HashTable có để sử dụng trong các ứng dụng đa luồng (multi-thread), nhưng ngày nay nó ít được sử dụng. Bởi vì, ConcurrentHashMap là lựa chọn tốt hơn HashTable.

Related posts:

Java Program to Implement Unrolled Linked List
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Bootstrap a Web Application with Spring 5
How to Replace Many if Statements in Java
Getting Started with GraphQL and Spring Boot
Java Program to Implement Binomial Heap
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Java Program to Implement Graph Structured Stack
Java Program to Check whether Graph is Biconnected
Java Program to Implement Gaussian Elimination Algorithm
Hướng dẫn Java Design Pattern – Prototype
Java Program to Implement Adjacency List
Java Program to Implement Miller Rabin Primality Test Algorithm
Using Java Assertions
Java 14 Record Keyword
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Implement Johnson’s Algorithm
Assert an Exception is Thrown in JUnit 4 and 5
New Features in Java 13
Java Program to Implement Circular Doubly Linked List
Java Program to Implement Repeated Squaring Algorithm
Java Program to Implement ScapeGoat Tree
Java Program to Implement AttributeList API
Java Program to Implement JobStateReasons API
Java Program to Implement Suffix Array
Get and Post Lists of Objects with RestTemplate
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Checked and Unchecked Exceptions in Java
Java Program to Implement Sorted Vector
Introduction to Spring Boot CLI
Spring Cloud Series – The Gateway Pattern
Serialize Only Fields that meet a Custom Criteria with Jackson