So sánh HashMap và HashSet 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à 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

HashSetHashMap
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:

Guide to the Synchronized Keyword in Java
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Java Program to Implement Rolling Hash
The Dining Philosophers Problem in Java
A Guide to JUnit 5 Extensions
Java Program to Compare Binary and Sequential Search
Giới thiệu SOAP UI và thực hiện test Web Service
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Spring Security OAuth2 – Simple Token Revocation
Java Program to Implement Gabow Algorithm
Check If a String Is Numeric in Java
JUnit5 Programmatic Extension Registration with @RegisterExtension
Java Program to Implement Nth Root Algorithm
Java Program to Perform Cryptography Using Transposition Technique
Java Program to Implement Booth Algorithm
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Jackson – Marshall String to JsonNode
Simultaneous Spring WebClient Calls
Java Program to Solve a Matching Problem for a Given Specific Case
A Custom Media Type for a Spring REST API
Service Registration with Eureka
Apache Tiles Integration with Spring MVC
Java Program to Implement vector
A Guide to TreeSet in Java
Java Program to Implement Caesar Cypher
Hướng dẫn Java Design Pattern – Visitor
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach