Collection Iteration
All collection types in Cangjie (ArrayList, HashSet, HashMap) implement the Iterable<T> interface, thus supporting for-in loop iteration. This is the most common way to traverse collections.
Collections also provide an iterator() method that returns an iterator object, calling next() to return Option<T> values one by one: Some(element) indicates there's still an element, None indicates the end of iteration. This approach is commonly used when you need manual control over the iteration process.
HashMap yields (key, value) tuples during iteration, which can be directly destructured in the for-in or while loop header using the (k, v) form to get both the key and value simultaneously.
The code on the right demonstrates three iteration methods: traditional for-in loop, while loop with an iterator (combined with pattern matching), and tuple destructuring iteration for HashMap.
while (let Some(x) <- iterator.next()) allows pattern matching in the condition, making the code more concise.