Collection Example
This example demonstrates how to use HashMap to count tag occurrences. This is a typical scenario of the "iterate, lookup, update" pattern.
Core logic: for each tag, first check if the key already exists in the HashMap. If it exists, retrieve the current count, increment it, and update; if it doesn't exist, insert an initial value of 1. After iterating through all tags, the HashMap records the occurrence count of each tag.
This pattern is common in scenarios like word frequency counting, vote counting, and group aggregation. HashMap's fast lookup characteristic (average O(1)) makes such operations very efficient.
The code on the right counts the frequency of occurrences in a tag array.
map[key], make sure the key exists, otherwise a runtime error will be triggered.Think about it: if the number of tags is very large (millions), how would this method perform?