The 'iterator()' method is a method in the Iterable interface. This method returns an object of a class that implements the Iterator interface. Since the Collection Interface extends the Iterable interface, it inherits the 'iterator()' method from it (the Iterable interface). And so, any object of a class that implements the Collection interface can call the 'iterator()' method.
Let's assume that I instantiate a class, that implements the Collection interface, like this:
List<String> names = new LinkedList<String>();
And after instantiation, I add objects, to the collection, like this:
names.add("John");
names.add("Joe");
Now, if I want an iterator object to iterate over the collection of String objects, I presumed that I could use the following syntax to get an iterator object:
Iterator<String> iterator = names.iterator();
However, I see a different syntax altogether, on a lot of websites, the syntax being Iterator iterator = names.iterator();
Which brings me to the question - Why do we see
Iterator iterator = names.iterator();
used over
Iterator<String> iterator = names.iterator();?
Is it because of backward compatibility? The way I see it, there ought to be a diamond notation after the word 'Iterator' e.g.
Iterator<String> iterator = names.iterator();
Links referred - Declaring an iterator in Java
By the way, is Peter Lawrey's answer to a question at Declaring an iterator in Java, the answer to my question? If yes, can somebody please expand on his answer? Thanks in advance.