In this example, let us create a generic type ArrayList to read in a few type string values and then iterate through those string elements and print them out on the screen.
List<String> greeting = new ArrayList<String>(); greeting.add("Hello"); greeting.add("world"); Iterator<String> greet = greeting.iterator(); while(greet.hasNext()) { System.out.println(greet.next()); }
The above program will create the following outcomes:
Hello world
There is no need to cast the return object from the next method of the Iterator