Trong các bài viết trước, tôi đã giới thiệu với các bạn HashMap và HashSet. Trong bài này, tôi sẽ so sánh sự giống nhau, khác nhau của 2 collection này.
1. Giống nhau của HashMap và HashSet
- Cả hai cấu trúc dữ liệu không duy trì bất kỳ thứ tự cho các phần tử truyền vào.
- Cả hai đều sử dụng phương thức hashCode () và equals () để duy trì tính duy nhất của dữ liệu.
- Cả hai cung cấp cho hiệu suất thời gian là hằng số cho các thao tác chèn (add/ put) và loại bỏ (remove).
- Cả hai đều không đồng bộ (non-synchronized).
2. Khác nhau của HashMap và HashSet
| HashSet | HashMap |
| HashSet cài đặt (implement) Set interface. | HashMap cài đặt (implement) Map interface. |
| HashSet lưu trữ dữ liệu dưới dạng các đối tượng (object). | HashMap lưu trữ dữ liệu dưới dạng cặp khóa-giá trị (key-value). |
| Bên trong HashSet sử dụng HashMap. | Bên trong HashMap sử dụng một mảng đối tượng Entry<K, V>. |
| HashSet không cho phép các phần tử trùng lặp. | HashMap không cho phép các khóa (key) trùng lặp, nhưng cho phép các giá trị (value) trùng lặp. |
| HashSet chỉ cho phép một phần tử null. | HashMap cho phép một khóa (key) null và nhiều giá trị (value) null. |
| Thao tác chèn (insert/add) chỉ yêu cầu một đối tượng. | Thao tác chèn (put) yêu cầu hai đối tượng, khóa và giá trị (key-value). |
| HashSet hơi chậm hơn HashMap. | HashMap nhanh hơn một chút so với HashSet. |
| Sử dụng HashSet khi bạn cần duy nhất dữ liệu (object). | Sử dụng HashSet khi bạn cần duy nhất khóa (key). |
Related posts:
The HttpMediaTypeNotAcceptableException in Spring MVC
Query Entities by Dates and Times with Spring Data JPA
Java Program to Find Path Between Two Nodes in a Graph
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java Program to Find Nearest Neighbor for Dynamic Data Set
Java Program to Implement Range Tree
Java Program to Implement Interpolation Search Algorithm
Java Program to Implement Euclid GCD Algorithm
Java Program to Represent Graph Using 2D Arrays
Java – Try with Resources
Collect a Java Stream to an Immutable Collection
Spring REST API + OAuth2 + Angular
Spring Data Reactive Repositories with MongoDB
Running Spring Boot Applications With Minikube
Java Program to Check whether Graph is a Bipartite using BFS
Hướng dẫn Java Design Pattern – Abstract Factory
Removing all duplicates from a List in Java
A Guide to System.exit()
XML-Based Injection in Spring
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Custom Thread Pools In Java 8 Parallel Streams
Composition, Aggregation, and Association in Java
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Implement DelayQueue API
Giới thiệu Json Web Token (JWT)
Programmatic Transaction Management in Spring
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Perform Quick Sort on Large Number of Elements
Java InputStream to Byte Array and ByteBuffer
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
New Features in Java 12