Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

Map of get vs containskey efficient check?

Asked by Cameron Oliver Jul 12, 2021 4.4K views 2 answers
Share

About this question

 I have map Map sampleMap ;

I have little confusion on the two map methods. i got a review saying that to use containsKey method rather than get method. But I am thinking in a way that the get will also check if the value is null or not ?

if(sampleMap.get('XXX') != null ) { //do stuff } if(sampleMap.containsKey('XXX')) { //do stuff }

Suggest me !


Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jul 3, 2024

When considering the efficiency of using get(key) versus containsKey(key) in a map data structure, such as HashMap or TreeMap, it's important to understand their underlying behaviors and performance characteristics:

  get(key) Method:

Behavior: Retrieves the value associated with the specified key if the key is present in the map; returns null if the key is not found.

Efficiency:

HashMap and LinkedHashMap:

Average-case time complexity is O(1) for retrieval due to hash-based lookup.

In rare cases, when there are hash collisions, it can degrade to O(n), but this is uncommon with a good hash function and proper load factor.

TreeMap:

  • Average-case time complexity is O(log n) due to its Red-Black Tree structure.
  • Guaranteed O(log n) performance ensures consistent behavior regardless of hash collisions.

containsKey(key) Method:

Behavior: Checks whether the map contains the specified key.

Efficiency:

HashMap and LinkedHashMap:

Average-case time complexity is O(1) due to hash-based lookup.

Similar to get(key), hash collisions are rare and can lead to O(n) in the worst case.

TreeMap:

Average-case time complexity is O(log n) due to tree traversal to check for the key's presence.

Key Differences and Considerations:

Use Cases:

Use get(key) when you need to retrieve the associated value for a key, as it directly returns the value if present.

Use containsKey(key) when you only need to check for the presence of the key without retrieving its associated value.

Performance Insights:

Both methods are efficient (O(1) or O(log n) depending on the map type), but get(key) involves retrieving the value, making it slightly heavier than containsKey(key), which only checks for existence.

Null Handling:

get(key) returns null if the key is not found or if the value associated with the key is null.

containsKey(key) returns true or false based solely on the presence of the key, irrespective of the associated value.

Implementation Differences:

get(key) is designed for retrieval and may involve additional operations to return the value, whereas containsKey(key) performs a straightforward existence check.

Example Usage:

  Map map = new HashMap<>();map.put("apple", 10);map.put("banana", 20);// Using get(key)Integer value1 = map.get("apple"); // returns 10Integer value2 = map.get("grape"); // returns null// Using containsKey(key)boolean containsApple = map.containsKey("apple"); // returns trueboolean containsGrape = map.containsKey("grape"); // returns false

Conclusion:

Efficiency: Both get(key) and containsKey(key) are generally efficient operations in hashmap implementations (O(1) average case). In tree-based maps like TreeMap, they are slightly heavier (O(log n) average case) due to tree traversal.

Choice: Choose based on whether you need to retrieve the associated value (get(key)) or simply check for key existence (containsKey(key)).

Performance Impact: For large datasets or critical performance scenarios, consider benchmarking or profiling to determine any noticeable performance differences between get(key) and containsKey(key) operations based on your specific usage patterns.

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.