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
| HashMap | Hashtable |
| 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:
Spring REST with a Zuul Proxy
Java Program to Implement Gauss Seidel Method
Jackson JSON Views
Java Program to Implement HashMap API
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Java Program to Implement Min Heap
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Implement Self organizing List
Spring Boot - Twilio
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Spring Boot - Sending Email
Shuffling Collections In Java
Logging a Reactive Sequence
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Evaluate an Expression using Stacks
A Guide to Java HashMap
Spring Boot - Cloud Configuration Client
Java Program to Implement Find all Back Edges in a Graph
A Quick JUnit vs TestNG Comparison
Spring Boot - Rest Controller Unit Test
Java – Rename or Move a File
Spring Data MongoDB – Indexes, Annotations and Converters
Prevent Brute Force Authentication Attempts with Spring Security
Java Program to Solve a Matching Problem for a Given Specific Case
A Custom Data Binder in Spring MVC
Java equals() and hashCode() Contracts
Spring RestTemplate Request/Response Logging
Exploring the Spring Boot TestRestTemplate
Spring AMQP in Reactive Applications
Java Program to Implement Warshall Algorithm
Java Program to Implement Floyd Cycle Algorithm
Java Program to Implement Wagner and Fisher Algorithm for online String Matching