Java HashMap
HashMap
is a hash table that stores key-value pairs. Keys are used to find values, just like indexes in arrays.HashMap
The advantage is that it can use any type as a key and the lookup is very fast.
Create a HashMap
// Import HashMap classimport ; public class Main { public static void main(String[] args) { // Create a HashMap object named capitalCities that will store the String key and String value HashMap<String, String> capitalCities = new HashMap<>(); } }
Add a project
// Add keys and values (country, city)("England", "London"); ("Germany", "Berlin"); ("Norway", "Oslo"); ("USA", "Washington DC");
Visit the project
// Get the capital of EnglandString capitalOfEngland = ("England");
Delete the project
// Delete the capital of England("England");
HashMap size
// Get the number of items in HashMapint size = ();
Looping through HashMap
// traverse HashMap and print keys and valuesfor (String key : ()) { String value = (key); ("Key: " + key + ", Value: " + value); }
Use other types
HashMap
Any type of keys and values can be stored. For example, you can storeInteger
Keys andString
value:
// Create a HashMap object named people that will store the Integer key and String valueHashMap<Integer, String> people = new HashMap<>(); // Add key and value (ID, name)(1, "John Doe"); (2, "Jane Doe"); // Get the name with ID 1String name = (1);
HashMap
It is a powerful data structure that can be used to store various types of data. It has fast lookup speed and flexible key-value pair storage mechanisms, making it ideal for many applications.
Advantages of HashMap:
- Quickly find speed
- Can store any type of keys and values
- Flexible key-value pair storage mechanism
Disadvantages of HashMap:
- Not thread-safe
- There may be a hash collision
suggestion:
- If you need to quickly find data, please use
HashMap
。 - If you need a thread-safe data structure, please use
ConcurrentHashMap
。 - If you need to avoid hash collisions, please use
LinkedHashMap
。
Java HashSet
HashSet
is an unordered collection where each element is unique. It is implemented based on a hash table, so the lookup is fast.
Create a HashSet
// Import HashSet classimport ; public class Main { public static void main(String[] args) { // Create a HashSet object named cars that will store the string HashSet<String> cars = new HashSet<>(); } }
Add a project
// Add a project("Volvo"); ("BMW"); ("Ford"); ("BMW"); // Will not be added repeatedly("Mazda"); // View HashSet(cars);
Check if the item exists
// Check whether the item existsboolean isPresent = ("Mazda"); // Output result("Is Mazda in HashSet?" + isPresent);
Delete the project
// Delete the project("Volvo"); // View HashSet(cars);
HashSet Size
// Get the HashSet sizeint size = (); // Output result("HashSet Size:" + size);
Looping through HashSet
// Loop through HashSetfor (String car : cars) { (car); }
Use other types
HashSet
Elements of any type can be stored. For example, you can storeInteger
element:
// Create a HashSet object named numbers that will store integersHashSet<Integer> numbers = new HashSet<>(); // Add elements(1); (2); (3); // Loop through HashSetfor (int number : numbers) { (number); }
HashSet
It is a very useful data structure that can be used to store various types of data. It has the characteristics of fast search speed and disorder, making it ideal for many applications.
Advantages of HashSet:
- Quickly find speed
- Unorder, allowing for faster addition and removal of elements
- Allows storage of any type of elements
Disadvantages of HashSet:
- The insertion order of elements is not preserved
- There may be a hash collision
suggestion:
- If you need to quickly find data and do not need to preserve the insertion order of elements, use
HashSet
。 - If you need to preserve the insertion order of elements, use
LinkedHashSet
。 - If you need to avoid hash collisions, please use
TreeMap
。
This is the end of this article about the efficient use skills of HashMap and HashSet in Java. For more related Java HashMap HashSet content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!