Iterating through Collections
Earlier in this chapter, when working with Exercise 01, Creating the AnalyzeInput Application we stopped when we were about to make searches through the data. We made it to the point where we had to iterate through the data and look for characteristics such as word frequency.
Iterators are used in Java to browse through collections. Let's look at a simple example that involves extracting the elements from a simple list one by one and printing them out.
import java.util.*; public class Example16 { Â Â Â Â public static void main(String[] args) { Â Â Â Â Â Â Â Â List<Integer> array = new ArrayList<>(); Â Â Â Â Â Â Â Â array.add(5); Â Â Â Â Â Â Â Â array.add(2); Â Â Â Â Â Â Â Â array.add(37); Â Â Â Â Â Â Â Â Iterator<Integer> iterator = array.iterator(...