0% found this document useful (0 votes)
13 views4 pages

HashMaps_and_HashSets_in_Java

Uploaded by

nnaveenjaimm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
13 views4 pages

HashMaps_and_HashSets_in_Java

Uploaded by

nnaveenjaimm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

HashMaps and HashSets in Java

Introduction to HashMaps and HashSets in Java

HashMaps and HashSets are part of Java's collection framework and provide efficient data storage

and retrieval using hashing.

HashMap: A collection that stores key-value pairs with unique keys.

HashSet: A collection that stores unique elements without any specific order.

HashMap

Definition: A data structure that implements the Map interface and stores key-value pairs.

Performance Characteristics:

- Access, Insert, Delete: Average O(1) due to hashing, though can degrade to O(n) in worst

cases.

Usage Scenarios:

- Efficient for fast retrievals by key.

- Not suitable for maintaining order of elements.

Example:

Map<String, Integer> hashMap = new HashMap<>();

hashMap.put("Alice", 23);

hashMap.put("Bob", 30);
hashMap.get("Alice"); // Returns 23

HashSet

Definition: A data structure that implements the Set interface, storing unique elements.

Performance Characteristics:

- Add, Remove, and Contains: Average O(1) due to hashing.

Usage Scenarios:

- Ideal for storing unique items and fast "contains" operations.

- Cannot store duplicate elements.

Example:

Set<String> hashSet = new HashSet<>();

hashSet.add("Apple");

hashSet.add("Banana");

hashSet.contains("Apple"); // Returns true

Key Differences Between HashMap and HashSet

- Storage: HashMap stores key-value pairs; HashSet only stores keys.

- Use of Nulls: HashMap allows one null key and multiple null values; HashSet allows one null

element.

- Duplicate Handling: HashMap keys must be unique; HashSet automatically handles duplicates.
Choosing Between HashMap and HashSet

Use HashMap if:

- You need to associate values with keys.

- Fast access by key is a priority.

Use HashSet if:

- You need a collection of unique elements.

- Fast checking for existence is needed without association.

Code Example Comparing HashMap and HashSet

HashMap<String, Integer> hashMap = new HashMap<>();

hashMap.put("John", 25);

hashMap.put("Alice", 30);

hashMap.put("Bob", 35);

HashSet<String> hashSet = new HashSet<>();

hashSet.add("Apple");

hashSet.add("Banana");

hashSet.add("Orange");

// Check existence in HashMap

boolean isInMap = hashMap.containsKey("John"); // true

// Check existence in HashSet


boolean isInSet = hashSet.contains("Apple"); // true

Conclusion

HashMap and HashSet are fundamental structures in Java for efficient data handling using hashing.

Understanding their characteristics aids in choosing the right one for specific applications.

You might also like