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:
How to Remove the Last Character of a String?
Configure a Spring Boot Web Application
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Limiting Query Results with JPA and Spring Data JPA
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Toán tử trong java
Jackson – Unmarshall to Collection/Array
Java Program to Implement Strassen Algorithm
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Spring Boot - Flyway Database
Constructor Dependency Injection in Spring
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Các kiểu dữ liệu trong java
Debug a HttpURLConnection problem
The Registration Process With Spring Security
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Custom Error Pages with Spring MVC
OAuth 2.0 Resource Server With Spring Security 5
Java Program to Implement Sparse Matrix
How to Iterate Over a Stream With Indices
Spring Webflux with Kotlin
Case-Insensitive String Matching in Java
Java Program to Check Whether a Given Point is in a Given Polygon
Java Program to Check Multiplicability of Two Matrices
Hướng dẫn Java Design Pattern – Bridge
Introduction to Spring Security Expressions
Spring Security Login Page with React
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Vector trong Java
Hướng dẫn tạo và sử dụng ThreadPool trong Java