Junior — Middle
Which interface in collections provides the ability to iterate over elements using an iterator?
sobes.tech AI
Answer from AI
In Java, an interface that provides the ability to iterate over collection elements using an iterator is called Iterable<T>. It contains the method iterator(), which returns an Iterator<T> object. Thanks to this interface, collections can be used in for-each loops.
Example:
List<String> list = Arrays.asList("a", "b", "c");
for (String item : list) {
System.out.println(item);
}
Here, list implements Iterable, allowing the use of this convenient syntax.