The iterators and enumeration guidelines in Apache Commons Collections

Apache Commons Collections is an open source Java library that provides many tools to operate and extend the set.These include two core interfaces: `Iterator` (iterator) and` enumeration` (enum).This guide will introduce the method of using iterators and enumers in Apache Commons Collections, and provide some Java code examples. The iterators and enum are used to traverse the set elements.The main difference between them is that enumers can only be used in the early versions of the Java set class, while the iterator can be used in the new version of the Java set class and the set class in the Apache Commons Collections. 1. Use the iterator to traverse the collection element The iterator is implemented through the `Iterator` interface.The following is an example code that shows how to use an iterator to traverse an ArrayList collection: List<String> list = new ArrayList<>(); list.add ("Element 1"); list.add ("Element 2"); list.add ("Element 3"); Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { String element = iterator.next(); System.out.println(element); } In the above code, we first created an ArrayList collection and added some elements.Then, we obtain an iterator object by calling the collection of the `Iterator ()" method.Next, use the `While` loop to traverse the elements in the collection.In each cycle, we use the `Next ()" method to obtain the current element and output it. 2. Use enum traversal set elements The enumerator is implemented through the `EnUrication` interface.The following is a sample code that shows how to use the enum to traverse a Vector set: Vector<Integer> vector = new Vector<>(); vector.add(1); vector.add(2); vector.add(3); Enumeration<Integer> enumeration = vector.elements(); while (enumeration.hasMoreElements()) { Integer element = enumeration.nextElement(); System.out.println(element); } In the above code, we first created a vector set and added some elements.Then, we obtain a enumerator object by calling the collection of `Elements ()` `methods.Next, use the `While` loop to traverse the elements in the collection.In each cycle, we use the `nextelement () method to obtain the current element and output it. It should be noted that because the enumerator is the leftover interface of the early Java collection class, its function is relatively limited.In the new Java collection class and Apache Commons Collection, it is strongly recommended to use iterators for collection operations. Summarize: The iterators and enumers in the Apache Commons Collection provides the method of traversing elements.The iterator is used in the new version of the Java set class and the set class in the Apache Commons Collections, and the enumerator is used in the early version of the Java set class.When using the iterator and the enumerator, you can use the `hasnext () method to determine whether there are still elements to traverse, and use the` next () (iterator) or `nextelement ()` (enumerator) methodGet the current element.In actual development, it is recommended to use the iterator for the collection operation to obtain more functions and flexibility. I hope this guide can help you know how to use iterators and enumers in Apache Commons Collections.If you have any questions, please ask at any time.